How to put link on the referenced node? like in node reference URL
I am trying to put the link using rules link to redirect to node/add it is not working , the redirecting url redirects to add content page where all the links to create content are shown

Comments

jonthomas83’s picture

I've been trying to figure out how to use "Flag" for this, but no such luck just yet.

Thanks for the great module but a link to "Create new content" from referenced nodes is essential in my opinion. Any ideas how we can do this?

If you need help writing documentation for this awesome module, let me know, just need to know what you want me to write! :)

jmary’s picture

My project is already using the module display suite (DS)(http://http://drupal.org/project/ds). As that stuff can do far much more that its name lets expect, I went to find a solution there. You don't need the Extra part of display suite, the basic one is enough.

In this example, I work with two node types : Contact and Note. I want to create notes which are referring to a contact. Also when a contact is displayed, I want to have a link "Create a note", sending me to node/add/note?field_refer_to=nid

It takes 2 minutes.

  1. Go to admin/structure/ds/fields/manage_ctools, choose a label and check node.
  2. Give a name to your field, for exemple : Create a refering note
  3. Go to admin/structure/types/manage/contact/fields
  4. Among the hidden fields you will find the field you created with DS. Put it somewhere, and now you have to edit what it display at clicking on the gear and then Edit Content.
  5. Choose : New custom content and in the body put : <a href="/node/add/note?field_refer_to=%node:nid">Create a Note</a>
  6. Click on Finish, then update, then save.
  7. You're done
kaizerking’s picture

@ jmary, I haven't tried this yet , can we limit the number of new nodes which can be created using this i.e limit the new nodes per reference per user?

jmary’s picture

I think it is possible at adding a code field in DS in place of a dynamic field.

But I think in this case, it becomes useless to use DS as the scope was to avoid to write any code.

Better to put the link directly in the nodetype template file at displaying it only when the test regarding "how many the user has created" is validating or not.

I won't cover that but the roadmap to do so is :

- write the code counting how many nodes of a given type the current user has created
- write a if block with your test
- display the link with the current node id

However this will only prevent the link to appear, it is still possible for the user to create some content at using the direct link /node/add/nodetype. You may try http://drupal.org/project/node_limit to avoid that.

kaizerking’s picture

in that case there is simpler solution reference dialog
it works conceptually very nice to create a node inline
The functional issue with any of this kind are:
1. it should have control on how many can be created
2. Node id should not be displayed along with the title, this gives a wrong idea to the creator issue here prevent display of node id after creation
3.Should not be extended to inline editing of newly created node
4. The link should disappear after create limit expires
5. I am aware of node limit, that cant be a solution but ideas can be picked up from there. because what we are talking is node limit per reference per user and not Node limit per user
May be you could pick some ideas from there instead of carry the weight of DS for this simple issue

EDIT: Safer approach, the newly created node only should be saved if the host node is saved. that would perfectly prevent spam but I doubt if that is possible some thing like create a node with a set default if like %%%%%=nid keep it till the save button of host node is clicked. save the new node before the host node is saved, then save the host node
Practically i don't know if this is possible, but I know there are applications other than drupal which use this kind

azuledu’s picture

rwilson0429’s picture

I was able to add a link on the referenced node using Views and EVA modules.
The EVA module allows you to attach a view to any entity using an EVA Field Display in the View.

  1. Create a View with an EVA Field display
  2. Under the Entity Content Section, configure the 'Entity type:' Node; Bundles: [YOUR-REFERENCED-CONTENT-TYPE]; Arguments: id
  3. Configure the View Contextual Filter to filter based on the Node ID of your Entity Reference Field by specifying a validation criteria When the filter value IS available or a default is provided.
  4. In the View's Header or Footer area (where you want your link to display), add a Global Text Area to display your link and put the following Entity Reference Prepopulate string in the global text area:

    '/node/add/[YOUR-CONTENT_TYPE]?[YOUR-ENTITY-REF-FIELD-MACHINE-NAME]=[nid]'

I wanted my user to be returned to the referenced entity after adding child content so, I used the following PHP snippet instead in my View Header:

<?php
//Use drupal_get_destination() function to direct the user back to the referring page after completing a form. See http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_get_destination/7 //
// If a destination exists in the previous request, that destination is returned. As such, a destination can persist across multiple pages.//

global $base_url;
$destination = drupal_get_destination();
$url = $base_url . '/node/add/[MY-CONTENT-TYPE]?[MY-ENTITY-REF-FIELD-MACHINE-NAME]=[nid]&';
$options = array(
  'query' => drupal_get_destination(),
  'attributes' => array(
    'class' => array('add-new'),
    'id' => 'add-new-[MY-CONTENT-TYPE]',
  ),
);
print l(t('Add new [MY-CONTENT-TYPE]'), $url, $options);
?>
sportel’s picture

Hi rwilson0429,

Thanks for your suggestions. I'm sorry for my noob question, but can you tell me where to put the php-snippit?

Thanks,

Mike.

rwilson0429’s picture

sportel, you can put the snippet in any Text Area in the View. I put the snippet in the View's global Footer section and added a Global:Text Area where I placed the code. Note, you must use the php code text format for the php code to be interpreted properly.

You don't need to include the php comments (lines starting with //) in the snippet, it's there for reference only.

There are so many ways to do things in Drupal. I'm certain there is a better way (don't like putting php in a View) but, this worked for me.
Hope that helps until a better solution is offered.

sportel’s picture

Thanks, works, simple, my mistake. I didn't have the php filter module (core) installed. Good work. But, I'm also interested in a more out of the box solution, instead of using php-code. For now, it works. Thanks.

Mike.

kaizerking’s picture

Use rules link its working

guy_schneerson’s picture

Quick fix using a block
The blow code adds an image to a gallery but you can easily replace the types for your use case

  1. Make sure you have PHP filter enabled
  2. Create a block set the text format to "PHP code"
  3. Paste the code blow (replacing "image", "field_gallery" & "gallery" with your types and fields )
  4. Set the region i.e content , content top ...
  5. Use the "Visibility settings" to set on correct pages and for relevant roles
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load( $nid );
  $type = $node->type;
  if (   $type == 'gallery' ) {
    $l = l( t('Add Image to gallery'), 'node/add/image',   array('query' => array('field_gallery' => $nid)  ));
    echo $l;   
  }
}
?>
yorguey31’s picture

I ve used the simple static field to do that it's pretty nice for adding non editable text such as 'a href'.
pointing to url we want. My problem is that i wanted to use token to build the url but this field does not support it.
I ve done some modifications on it and it work well !!
unfortunately i'm not able to reverse that to community because i dont know anything about Git...
If someone want to add it, it will be nice.

function static_field_field_instance_settings_form($field, $instance) {
 $settings = $instance['settings'];
  $form = array();

  $form['text'] = array(
    '#type' => 'text_format',
    '#title' => t('Static field content'),
    '#default_value' => $settings['text']['value'],
    '#format' => $settings['text']['format'],
    '#required' => TRUE,
    '#description' => t('The static field text to display in this instance of field.')
  );
  //ADD
  if (module_exists('token')) {
    // Add token module replacements fields
    $form['tokens'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Placeholder tokens'),
      '#description' => t("The following placeholder tokens can be used in content, they will be replaced with the appropriate values."),
    );
    $entity_info = entity_get_info($instance['entity_type']);
    $form['tokens']['help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array($entity_info['token type']),
      '#global_types' => TRUE,
      '#click_insert' => TRUE,
    );
  }
  //ADD END
  $form['form_display'] = array(
    '#type' => 'radios',
    '#title' => t('Display also in form'),
    '#description' => t("Select how the static content will be displayed in the edit form."),
    '#required' => TRUE,
    '#default_value' => $settings['form_display'],
    '#options' => array(
      FALSE => t('No'),
      'markup' => t('Yes. No label'),
      'item' => t('Yes. Display also the label'),
    ),
  );
  return $form;
}
and 
function static_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  //ADD and MODIFY
 <strong> if (empty($instance['settings']['text']['value'])) {
    return;
  }
  // Replace URL tokens.
  $entity_type = $instance['entity_type'];
  $entity_info = entity_get_info($entity_type);
  $property_id = $entity_info['entity keys']['id'];
  $entity_token_type = isset($entity_info['token type']) ? $entity_info['token type'] : (
    $entity_type == 'taxonomy_term' || $entity_type == 'taxonomy_vocabulary' ? str_replace('taxonomy_', '', $entity_type) : $entity_type
  );
  global $user;
  // Load the entity if necessary for entities in views.
  if (isset($entity->{$property_id})) {
    $entity_loaded = entity_load($entity_type, array($entity->{$property_id}));
    $entity_loaded = array_pop($entity_loaded);
  }
  else {
    $entity_loaded = $entity;
  }
  $text_markup=check_markup($instance['settings']['text']['value'], $instance['settings']['text']['format']);
  $static_text=token_replace($text_markup,array($entity_token_type => $entity_loaded));

  $item_static = array('#markup' => $static_text);
  //END ADD and modify
  
  foreach ($items as $delta => $item) {
    $element[$delta] = $item_static;
  }
  $element[0] = $item_static;
  return $element;

Hopes taht will help !

Anonymous’s picture

@yorguey31
where do you put the attached code?
thanks in advance

hickimse’s picture

#12 working

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load( $nid );
  $type = $node->type;
  if (   $type == 'gallery' ) {
    $l = l( t('Add Image to gallery'), 'node/add/image',   array('query' => array('field_gallery' => $nid)  ));
    echo $l;
  }
}
?>

How can ı do


$l = l( t('Add Image to gallery'), 'node/add/image',  <== image content type should be all content type  
I try    $l = l( t('Add Image to gallery'), 'node/add/',  open navigation menu and select but dont fill the reference field 

How do I make it

jiakomo’s picture

I use views to create a block in order to create links and I use a contextual filter in order to use the nid of the referenced node from the url.

rakesh.nimje84@gmail.com’s picture

Issue summary: View changes

Add one field (as link) to the content type with link module and use token to set url. It's simple solution working in my case.

autopoietic’s picture

Was about to make a simple block module and saw #6.

Thanks @azuledu, thats really neat. I would like to see it incorporated into the entity reference prepopulate module personally.

apmsooner’s picture

This is dead simple using context from view or panel.

In a view for example, you can use tokens from contextual filter like this:
<a href="/node/add/plan?field_client_reference=!1&destination=node/!1">Add Plan</a>

- field_client_reference = the machine name of my entity reference field
- !1 = the token value for my contextual filter

no need for php or other module if this is all you need.

leramulina’s picture

rwilson0429

I used your recipe but instead leading to the page

node/add/node-gallery-item?node_gallery_ref_1=1176

the link leads me to the page

node/add/node_gallery_item?node_gallery_item=[nid]&&destination=node/1176

Can you tell me, please, where I could be wrong?

The code in text field is

global $base_url;
$destination = drupal_get_destination();
$url = $base_url . '/node/add/node-gallery-item?node_gallery_ref_1=[nid]&';
$options = array(
  'query' => drupal_get_destination(),
  'attributes' => array(
    'class' => array('add-new'),
    'id' => 'add-new-node-gallery-item',
  ),
);
print l(t('Add new node-gallery-item'), $url, $options);

There are

Arguments:id

Contextual filters
Content: Nid
When the filter value is NOT available
Display all results for the specified field
When the filter value IS available or a default is provided
Specify validation criteria
Validator:
Content
Content types:
Gallery

Thanks!

tonyoh’s picture

leramulina,

I also couldn'y get it working and had to put the name of my site ... ie the full path into $url.
www.mysite.com/node/add/node-gallery-item.......

Sorry I can't help further.

apmsooner’s picture

Set context to provide default value : content id from url

schifazl’s picture

@leramulina I also had problems with this, but finally I checked "Use replacement tokens from the first row" under the text field and replaced [nid] with !1

I know that this is old, but maybe it could be useful for someone :)

paulwdru’s picture

After lengthy discussion for about 4 years, is maintainer aware of what's being critically missing in Entity Reference Prepopulate as compared to Node Reference URL Widget ? Thanks

alauddin’s picture

Yes, its amazing we still dont have a module to do this after all this time.

here is a snippet of code that I use with Display Suite - Extras sub-module
1) enable module - than go to admin/structure/ds/list/extras
2) Check Enable Extra fields
3) create code field - admin/structure/ds/fields
3a) attach to 'node' entities
3b) filter if you want to only the content type that this field should display on
3c) field code: php filter

$node = menu_get_object(); //get parent node object
$link_txt = t('Add gallery image'); //set text for link and title attribute
print l($link_txt, 'node/add/gallery-image', array('attributes' => array('title' => $link_txt), 'query' => array('field_gallery_ref' => $node -> nid)));

4) change link text
change node/add/xxx to your content type
change field_gallery_ref to your entity ref field found on the node you are creating.

5) Go to 'manage display' of your referenced content type and you should see the ds field...place it where you like and prosper :)