entity_translation_handler_field_label.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * This file contains a label field handler for entity translation.
  5. */
  6. /**
  7. * This handler shows the entity label for entities in the entity_translation table.
  8. */
  9. class entity_translation_handler_field_label extends views_handler_field {
  10. function construct() {
  11. parent::construct();
  12. $this->additional_fields['entity_id'] = 'entity_id';
  13. $this->additional_fields['entity_type'] = 'entity_type';
  14. }
  15. function query() {
  16. $this->ensure_my_table();
  17. $this->add_additional_fields();
  18. }
  19. /**
  20. * Add a 'link to entity' option definition.
  21. * @see views_handler_field::option:definition()
  22. */
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. $options['link_to_entity'] = array('default' => '', 'translatable' => FALSE);
  26. return $options;
  27. }
  28. /**
  29. * Add a 'link to entity' option.
  30. * @see views_handler_field::options_form()
  31. */
  32. function options_form(&$form, &$form_state) {
  33. parent::options_form($form, $form_state);
  34. $form['link_to_entity'] = array(
  35. '#title' => t('Link this field to it\'s entity'),
  36. '#type' => 'checkbox',
  37. '#default_value' => $this->options['link_to_entity']
  38. );
  39. }
  40. /**
  41. * Load all entities, so that we can get the label.
  42. */
  43. function post_execute(&$values) {
  44. $ids = array();
  45. $ids_by_type = array();
  46. foreach ($values as $row) {
  47. if ($entity_type = $this->get_value($row, 'entity_type')) {
  48. $ids_by_type[$entity_type][] = $this->get_value($row, 'entity_id');
  49. }
  50. }
  51. foreach ($ids_by_type as $type => $ids) {
  52. $this->entities[$type] = entity_load($type, $ids);
  53. }
  54. }
  55. function render($values) {
  56. $entity_type = $this->get_value($values, 'entity_type');
  57. $entity_id = $this->get_value($values, 'entity_id');
  58. // Check if entity is not empty
  59. if (!$entity_id || !$entity_type) {
  60. return NULL;
  61. }
  62. $entity = $this->entities[$entity_type][$entity_id];
  63. // We could also use entity_label(), but since this we might want to let
  64. // the handler decide what's best to show.
  65. $handler = entity_translation_get_handler($entity_type, $entity);
  66. $label = $handler->getLabel();
  67. if ($this->options['link_to_entity']) {
  68. $this->options['alter']['make_link'] = TRUE;
  69. $this->options['alter']['path'] = $handler->getViewPath();
  70. }
  71. return $label;
  72. }
  73. }