views_handler_title_field.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Field handler to present field title with link to the entity.
  4. *
  5. * @ingroup views_field_handlers
  6. */
  7. class views_handler_title_field extends views_handler_field_field {
  8. function option_definition() {
  9. $options = parent::option_definition();
  10. $options['link_to_entity'] = array('default' => isset($this->definition['link_to_entity default']) ? $this->definition['link_to_entity default'] : FALSE);
  11. return $options;
  12. }
  13. /**
  14. * Provide link to entity option.
  15. */
  16. function options_form(&$form, &$form_state) {
  17. $form['link_to_entity'] = array(
  18. '#title' => t('Link this field to the original entity'),
  19. '#description' => t("Enable to override this field's links."),
  20. '#type' => 'checkbox',
  21. '#default_value' => !empty($this->options['link_to_entity']),
  22. );
  23. parent::options_form($form, $form_state);
  24. }
  25. function advanced_render($values) {
  26. $this->original_values = $values;
  27. return parent::advanced_render($values);
  28. }
  29. function render_item($count, $item) {
  30. if (!empty($this->options['link_to_entity'])) {
  31. $values = $this->original_values;
  32. $entity_type = $this->definition['entity_tables'][$this->base_table];
  33. $key = $this->field_alias;
  34. if (!empty($values->_field_data[$key]['entity'])) {
  35. $entity = $values->_field_data[$key]['entity'];
  36. $uri = entity_uri($entity_type, $entity);
  37. $this->options['alter']['make_link'] = TRUE;
  38. $this->options['alter']['path'] = $uri['path'];
  39. $this->options['alter']['options'] = !empty($uri['options']) ? $uri['options'] : array();
  40. }
  41. }
  42. return parent::render_item($count, $item);
  43. }
  44. }