2

I have two content types, Parent and Child. Parent contains a list of tagged users which may create Child. Child contains the tag of the Parent.

On the Parent page I need a link, which is only visible to people tagged in it, to create a new Child with the Parent already tagged in it.

I know I need to use something like Entity Reference Prepopulate but my problem is that I can't figure out how to make the link.

This seems like it should be a simple thing to do but I can't for the life of me figure out how!

The requirement is that this should only use the UI and without editing any .php files as there is no guarantee that other devs will be able to code and allows easy visual recognition of what has changed.

Any advice would be greatly appreciated.

9
  • 1
    There are 100 ways fo doing this, but I can't think of one without editing a php file at least once. You can tell your devs to install a module you have and they don't need to edit any php, but not without you making a module. Sorry
    – reptilex
    Commented Oct 15, 2012 at 15:18
  • Would this be possible using Views PHP?
    – dav_i
    Commented Oct 16, 2012 at 8:54
  • Where do you want to display create child page link ? In Node Page Or in views results ? How are you tagging users to the parent node ? using entity reference field ?
    – Anil Sagar
    Commented Oct 16, 2012 at 9:51
  • @AnilSagar Either in node page or views would be fine. And yes, users are tagged by entity reference field.
    – dav_i
    Commented Oct 16, 2012 at 11:02
  • I don't understand why your developers can't install a module you built. If you make a module and DO edit a few php files, they just need to install it, not recode it. And no views_php is not enough, because you could write enough php for the link to appear, but not for the form to be populated with the parent.
    – reptilex
    Commented Oct 16, 2012 at 16:00

1 Answer 1

0

So here is how to do it.

  1. Install and activate Entity Reference Prepopulate and enable it within the Entity Reference of Parents field in Child. You can now populate that field by example.com/node/add/child?field_parents=[parent nid].
  2. Install and activate Views PHP.
  3. Create a new View without any pages or blocks called "Create new Child"
  4. Click "+Add" and select "Content Pane"
  5. Apply the following settings:
    • Filter critera
      • Content: Published (Yes)
      • Content: Type (= Parent)
    • Pane settings
      • Link to view: Yes
      • Argument input: From context>Node Content ID
    • Contextual filters (under advanced)
      • Content: Nid
  6. Set Format - Show: Fields and then add a field - Global: PHP
  7. In the configuration for Global: PHP, in the Output code section write: <?php print '<a href="http://example.com/node/add/child?field_parent=' . (string)$data->nid . '">Create new child</a>'; ?>. (N.B. You need title field/NID field added as well for the %data->nid to work, hide it if you wish)
  8. Add a Global: PHP in "Filter Criteria" and add the following code

    $uid = $GLOBALS["user"]->uid;
    $node = node_load($data->nid);
    $info = field_get_items('node',$node,'field_users');
    $uids = array();
    foreach($info as $user)
    {
    $uids[] = $user['target_id'];
    }
    return in_array($uid, $uids) ? FALSE : TRUE;

  9. Save everything. In in Pages now, you can add this into Panels via "View Panes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.