entity_translation_handler_field_translate_link.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * Translate link plugin.
  5. */
  6. /**
  7. * This handler adds translate link for all translatable entities.
  8. */
  9. class entity_translation_handler_field_translate_link 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. $this->additional_fields['language'] = 'language';
  15. }
  16. /**
  17. * Add required additional fields.
  18. */
  19. function query() {
  20. $this->ensure_my_table();
  21. $this->add_additional_fields();
  22. }
  23. /**
  24. * Add the text option.
  25. * @see views_handler_field::option_definition()
  26. */
  27. function option_definition() {
  28. $options = parent::option_definition();
  29. $options['text'] = array('default' => '', 'translatable' => TRUE);
  30. return $options;
  31. }
  32. /**
  33. * Add the option to set the title of the translate link.
  34. * @see views_handler_field::options_form()
  35. */
  36. function options_form(&$form, &$form_state) {
  37. $form['text'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Text to display'),
  40. '#default_value' => $this->options['text'],
  41. );
  42. parent::options_form($form, $form_state);
  43. // The path is set by render_link function so don't allow setting it.
  44. $form['alter']['path'] = array('#access' => FALSE);
  45. $form['alter']['external'] = array('#access' => FALSE);
  46. }
  47. /**
  48. * Load all entities based on the data we have.
  49. */
  50. function post_execute(&$values) {
  51. $ids = array();
  52. $ids_by_type = array();
  53. foreach ($values as $row) {
  54. if ($entity_type = $this->get_value($row, 'entity_type')) {
  55. $ids_by_type[$entity_type][] = $this->get_value($row, 'entity_id');
  56. }
  57. }
  58. foreach ($ids_by_type as $type => $ids) {
  59. $this->entities[$type] = entity_load($type, $ids);
  60. }
  61. }
  62. /**
  63. * @see views_handler_field::render()
  64. */
  65. function render($values) {
  66. $type = $this->get_value($values, 'entity_type');
  67. $entity_id = $this->get_value($values, 'entity_id');
  68. // Check if entity is not empty
  69. if (!$entity_id || !$type) {
  70. return NULL;
  71. }
  72. $language = $this->get_value($values, 'language');
  73. $entity = $this->entities[$type][$entity_id];
  74. return $this->render_link($type, $entity_id, $entity, $language);
  75. }
  76. /**
  77. * Render the link to the translation overview page of the entity.
  78. */
  79. function render_link($entity_type, $entity_id, $entity, $language) {
  80. if (!entity_translation_tab_access($entity_type, $entity)) {
  81. return;
  82. }
  83. // We use the entity info here to avoid having to call entity_load() for all
  84. // the entities.
  85. $info = entity_get_info($entity_type);
  86. $path = $info['translation']['entity_translation']['path schemes']['default']['translate path'];
  87. $path = str_replace($info['translation']['entity_translation']['path schemes']['default']['path wildcard'], $entity_id, $path);
  88. $this->options['alter']['make_link'] = TRUE;
  89. $this->options['alter']['path'] = $path;
  90. $this->options['alter']['query'] = drupal_get_destination();
  91. $text = !empty($this->options['text']) ? $this->options['text'] : t('translate');
  92. return $text;
  93. }
  94. }