1

I have a content type (Sample) which includes a field (A) whose widget type is View. I'm using the Entity Reference View Widget View module for this.

The output of this field is having only the title of the node added from the view, but I want also the Node Edit link.

As far as I know, the output of Entiy Reference View Widget View can be printed with any custom view mode. I'm trying to add the node edit link in this custom view mode.

Do you have any idea on how to add a custom field having the edit link in the view mode?The steps which i followed are:

Step 1:I selected the particular view mode in the content type field settings A: Here is the screen shot:

enter image description here

Step 2: In Display suit fields(admin/structure/ds/fields) i have added custom edit link field like <a href="node/edit/[node:nid]">Edit</a> I can see Edit link in the output ,but the token value i provided [node:nid] is not fetching the node id. Link is redirecting to node/edit/%5Bnode%3Anid%5D .Can anybody help me in this? Please the below screenshot enter image description here

Correct me if i'm wrong. Any other approach or method is also fine.

2

Entity reference widget modal with edit link

You can enable views_ui in module page, then go to structure/views and edit the views you are using for the widget. Add Content: Edit link in fields and save it. It will got the edit links for each entity as above image.


Override theme function by custom module

There is no easy way to add the edit link for the widget on node page. But you can still make it happen by overriding the theme functions provided by Entity Reference View Widget View

Entity reference widget with edit link on node edit page

Below is the sample snippet with assuming the name of your custom module is mymodule. You can copy and paste it into a custom module for testing. (Remember to change the mymodule if you have different name for your custom module.)

<?php

/**
 * Implements hook_theme_registry_alter().
 */
function mymodule_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['entityreference_view_widget_widget']['function'])) {
    $theme_registry['entityreference_view_widget_widget']['function'] = 'mymodule_entityreference_view_widget_widget';
  }
  if (isset($theme_registry['entityreference_view_widget_rows']['function'])) {
    $theme_registry['entityreference_view_widget_rows']['function'] = 'mymodule_entityreference_view_widget_rows';
  }
} 

/**
 * Override theme_entityreference_view_widget_widget().
 */
function mymodule_entityreference_view_widget_widget($variables) {
  $widget = $variables['widget'];
  $settings = $widget['add_more']['#ervw_settings'];
  $order_class = drupal_html_class($settings['element'] . '-delta-order');
  $required = !empty($settings['instance']['required']) ? theme('form_required_marker', $variables) : '';

  $header = array(
    array(
      'data' => '<label>' . t('!title !required', array('!title' => $widget['#title'], '!required' => $required)) . "</label>",
      'colspan' => 2,
      'class' => array('field-label'),
    ),
  );
  $header[] = array(
    'data' => "<label>" . t('Edit link') . "</label>",
    'class' => array('field-label'),
  );
  if ($settings['field']['cardinality'] !== '1') {
    $header[] = array(
      'data' => t('Order'),
      'class' => array('tabledrag-hide'),
    );
  }
  $rows = array();

  // Sort items according to '_weight' (needed when the form comes back after
  // preview or failed validation)
  $items = array();
  foreach (element_children($widget) as $key) {
    if ($key === 'add_more') {
      $add_more_button = &$widget[$key];
    }
    else {
      $items[] = &$widget[$key];
    }
  }
  usort($items, '_field_sort_items_value_helper');

  // Add the items as table rows.
  foreach ($items as $item) {
    $item['_weight']['#attributes']['class'] = array($order_class);
    $delta_element = drupal_render($item['_weight']);

    $cells = array(
      array('data' => '', 'class' => array('field-multiple-drag')),
      drupal_render($item),
    );

    $target_id = $item['target_id']['#return_value'];
    $entities = entity_load($settings['target_type'], array($target_id));
    $cells[] = array(
      'data' => _mymodule_entity_edit_link($settings['target_type'], $entities[$target_id]),
    );

    if ($settings['field']['cardinality'] !== '1') {
      $cells[] = array('data' => $delta_element, 'class' => array('delta-order'));
    }
    $rows[] = array(
      'data' => $cells,
      'class' => array('draggable'),
    );
  }

  if (empty($rows)) {
    $rows[][] = array(
      'data' => t('No items have been added yet. Click "Add items" to launch the widget.'),
      'colspan' => '3',
    );
  }

  $output = '<div class="form-item">';
  $form_errors = form_get_errors();
  $classes = array('field-multiple-table');

  // Add an error class to the table in case of error.
  if (isset($form_errors[$settings['element']])) {
    $classes[] = 'error';
  }

  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => $settings['table_id'],
      'class' => $classes,
    ),
  ));
  $output .= $widget['#description'] ? '<div class="description">' . $widget['#description'] . '</div>' : '';
  $output .= '<div class="clearfix">' . drupal_render($add_more_button) . '</div>';
  $output .= '</div>';

  if ($settings['field']['cardinality'] !== '1') {
    drupal_add_tabledrag($settings['table_id'], 'order', 'sibling', $order_class);
  }

  return $output;
}

/**
 * Override theme_entityreference_view_widget_rows().
 */
function mymodule_entityreference_view_widget_rows($variables) {
  $zebra = array('even' => 'odd');

  $settings = $variables['rows']['#widget_settings'];
  foreach (element_children($variables['rows']) as $row) {
    $output[$row]  = '<tr class="draggable ' . current($zebra) . '">';
    $output[$row] .= '<td class="field-multiple-drag"></td>';
    $output[$row] .= '<td>' . render($variables['rows'][$row][$settings['column']]) . '</td>';

    $target_id = $variables['rows'][$row]['target_id']['#return_value'];
    $entities = entity_load($settings['target_type'], array($target_id));
    $output[$row] .= '<td>' . _mymodule_entity_edit_link($settings['target_type'], $entities[$target_id]) . '</td>';

    if ($settings['cardinality'] !== '1') {
      $output[$row] .= '<td class="delta-order tabledrag-hide">' . render($variables['rows'][$row]['_weight']) . '</td>';
    }
    $output[$row] .= '</tr>';

    $zebra = array_flip($zebra);
  }

  return implode("\n", $output);
}

/**
 * Helper function to build the entity edit link.
 */
function _mymodule_entity_edit_link($entity_type, $entity) {
  $uri = entity_uri($entity_type, $entity);
  return l(t('Edit'), $uri['path'] . '/edit');
}

The key concept here is programmatically override the 2 theme functions with hook_theme_registry_alter:

  • theme_entityreference_view_widget_widget()
  • theme_entityreference_view_widget_rows()

So we can now modify the table output.

Add a new column header

$header[] = array(
  'data' => "<label>" . t('Edit link') . "</label>",
  'class' => array('field-label'),
);

Add link for new column

For static output.

$target_id = $item['target_id']['#return_value'];
$entities = entity_load($settings['target_type'], array($target_id));
$cells[] = array(
  'data' => _mymodule_entity_edit_link($settings['target_type'], $entities[$target_id]),
);

For dynamic output. (ajax callback)

$target_id = $variables['rows'][$row]['target_id']['#return_value'];
$entities = entity_load($settings['target_type'], array($target_id));
$output[$row] .= '<td>' . _mymodule_entity_edit_link($settings['target_type'], $entities[$target_id]) . '</td>';

Helper function to build edit link

/**
 * Helper function to build the entity edit link.
 */
function _mymodule_entity_edit_link($entity_type, $entity) {
  $uri = entity_uri($entity_type, $entity);
  return l(t('Edit'), $uri['path'] . '/edit');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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