views_handler_field_contextual_links.inc

Definition of views_handler_field_contextual_links.

File

handlers/views_handler_field_contextual_links.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_contextual_links.
  5. */
  6. /**
  7. * Provides a handler that adds contextual links.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_contextual_links extends views_handler_field {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['fields'] = array('default' => array());
  15. $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
  16. return $options;
  17. }
  18. function options_form(&$form, &$form_state) {
  19. $all_fields = $this->view->display_handler->get_field_labels();
  20. // Offer to include only those fields that follow this one.
  21. $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
  22. $form['fields'] = array(
  23. '#type' => 'checkboxes',
  24. '#title' => t('Fields'),
  25. '#description' => t('Fields to be included as contextual links.'),
  26. '#options' => $field_options,
  27. '#default_value' => $this->options['fields'],
  28. );
  29. $form['destination'] = array(
  30. '#type' => 'checkbox',
  31. '#title' => t('Include destination'),
  32. '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
  33. '#default_value' => $this->options['destination'],
  34. );
  35. }
  36. function pre_render(&$values) {
  37. // Add a row plugin css class for the contextual link.
  38. $class = 'contextual-links-region';
  39. if (!empty($this->view->style_plugin->options['row_class'])) {
  40. $this->view->style_plugin->options['row_class'] .= " $class";
  41. }
  42. else {
  43. $this->view->style_plugin->options['row_class'] = $class;
  44. }
  45. }
  46. /**
  47. * Render the contextual fields.
  48. */
  49. function render($values) {
  50. $links = array();
  51. foreach ($this->options['fields'] as $field) {
  52. if (empty($this->view->style_plugin->rendered_fields[$this->view->row_index][$field])) {
  53. continue;
  54. }
  55. $title = $this->view->field[$field]->last_render_text;
  56. $path = '';
  57. if (!empty($this->view->field[$field]->options['alter']['path'])) {
  58. $path = $this->view->field[$field]->options['alter']['path'];
  59. }
  60. if (!empty($title) && !empty($path)) {
  61. // Make sure that tokens are replaced for this paths as well.
  62. $tokens = $this->get_render_tokens(array());
  63. $path = strip_tags(decode_entities(strtr($path, $tokens)));
  64. $links[$field] = array(
  65. 'href' => $path,
  66. 'title' => $title,
  67. );
  68. if (!empty($this->options['destination'])) {
  69. $links[$field]['query'] = drupal_get_destination();
  70. }
  71. }
  72. }
  73. if (!empty($links)) {
  74. $build = array(
  75. '#prefix' => '<div class="contextual-links-wrapper">',
  76. '#suffix' => '</div>',
  77. '#theme' => 'links__contextual',
  78. '#links' => $links,
  79. '#attributes' => array('class' => array('contextual-links')),
  80. '#attached' => array(
  81. 'library' => array(array('contextual', 'contextual-links')),
  82. ),
  83. '#access' => user_access('access contextual links'),
  84. );
  85. return drupal_render($build);
  86. }
  87. else {
  88. return '';
  89. }
  90. }
  91. function query() { }
  92. }