rules_link.views.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * Provide views data.
  5. */
  6. /**
  7. * Implements hook_views_data().
  8. */
  9. function rules_link_views_data() {
  10. $data = array();
  11. foreach (rules_link_get_links() as $name => $rules_link) {
  12. $entity = entity_get_info($rules_link->entity_type);
  13. $data[$entity['base table']][$name]['field']['moved to'] = array('views_entity_' . $rules_link->entity_type, $name, 'field');
  14. $data['views_entity_' . $rules_link->entity_type][$name]['field'] = array(
  15. 'title' => $rules_link->label,
  16. 'help' => t('Execute the selected rules link.'),
  17. 'handler' => 'views_handler_field_rules_link',
  18. 'rules link name' => $name,
  19. );
  20. }
  21. return $data;
  22. }
  23. /**
  24. * Renders the links for views.
  25. */
  26. class views_handler_field_rules_link extends views_handler_field_entity {
  27. function render($value) {
  28. $replacements = filter_xss_admin($this->options['rules_link']['rules_link_parameters']);
  29. $token = $this->get_render_tokens('');
  30. $parameters = array_filter(explode(',', strtr($replacements, $token)));
  31. // Trim the spaces from the
  32. array_walk($parameters, 'rules_link_trim_parameters');
  33. $rules_link = rules_link_load($this->definition['rules link name']);
  34. $entity = $this->get_value($value);
  35. list($id, $rev, $bundle) = entity_extract_ids($rules_link->entity_type, $entity);
  36. // If there are multiple bundle types, we only have to display the links to
  37. // the entities which have the correct bundle.
  38. if (empty($rules_link->settings['bundles']) || in_array($bundle, $rules_link->settings['bundles'])) {
  39. $output = rules_link_render_link($rules_link, $id, drupal_get_destination(), $parameters);
  40. if (isset($this->options['rules_link']['rules_link_rewrite']) && !empty($this->options['rules_link']['rules_link_rewrite'])) {
  41. $output['#options']['html'] = TRUE;
  42. $output['#title'] = strtr($this->options['rules_link']['rules_link_rewrite'], $this->get_render_tokens(''));
  43. }
  44. return $output;
  45. }
  46. }
  47. /**
  48. * Override option_definition.
  49. */
  50. function option_definition() {
  51. $options = parent::option_definition();
  52. $options['rules_link']['rules_link_parameters'] = array('default' => '');
  53. $options['rules_link']['rules_link_rewrite'] = array('default' => '');
  54. return $options;
  55. }
  56. /**
  57. * Override options_form.
  58. */
  59. function options_form(&$form, &$form_state) {
  60. parent::options_form($form, $form_state);
  61. $form['rules_link'] = array(
  62. '#type' => 'fieldset',
  63. '#title' => t('Rules Link settings'),
  64. '#collapsible' => TRUE,
  65. '#collapsed' => TRUE,
  66. );
  67. $form['rules_link']['rules_link_parameters'] = array(
  68. '#type' => 'textfield',
  69. '#title' => t('Additional parameters for the rules link'),
  70. '#description' => t('Enter additional parameter using replacement patterns, use comma to enter multiple items. Whitespaces at the beginning and end of token will be trimmed.'),
  71. '#default_value' => $this->options['rules_link']['rules_link_parameters'],
  72. );
  73. $form['rules_link']['rules_link_rewrite'] = array(
  74. '#type' => 'textarea',
  75. '#title' => t('Rewrite the link text'),
  76. '#description' => t('Enter a custom link title or HTML that you want to place inside of the rules link. You may use the replacement patterns below.'),
  77. '#default_value' => $this->options['rules_link']['rules_link_rewrite'],
  78. );
  79. $form['rules_link']['rules_link_patterns'] = array(
  80. '#type' => 'fieldset',
  81. '#title' => t('Replacement patterns'),
  82. '#collapsible' => TRUE,
  83. '#collapsed' => TRUE,
  84. '#value' => $form['alter']['help']['#value'],
  85. );
  86. }
  87. }