EntityLabel.php 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\views\Plugin\views\field\EntityLabel.
 */

namespace Drupal\views\Plugin\views\field;

10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\views\Plugin\views\field\FieldPluginBase;
13
use Drupal\views\ResultRow;
14 15 16 17 18 19 20
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Field handler to display entity label optionally linked to entity page.
 *
21
 * @ViewsField("entity_label")
22 23 24 25 26 27 28 29 30 31 32 33 34
 */
class EntityLabel extends FieldPluginBase {

  /**
   * Array of entities that reference to file.
   *
   * @var array
   */
  protected $loadedReferencers = array();

  /**
   * EntityManager class.
   *
35
   * @var \Drupal\Core\Entity\EntityManagerInterface
36 37 38 39 40 41 42 43 44 45
   */
  protected $entityManager;

  /**
   * Constructs a EntityLabel object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
46
   * @param mixed $plugin_definition
47
   *   The plugin implementation definition.
48
   * @param \Drupal\Core\Entity\EntityManagerInterface $manager
49 50
   *   EntityManager that is stored internally and used to load nodes.
   */
51
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $manager) {
52 53 54 55 56 57 58 59
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->entityManager = $manager;
  }

  /**
   * {@inheritdoc}
   */
60
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61 62 63 64
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
65
      $container->get('entity.manager')
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    );
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->additional_fields[$this->definition['entity type field']] = $this->definition['entity type field'];
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['link_to_entity'] = array('default' => FALSE);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
89
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
90
    $form['link_to_entity'] = array(
91 92
      '#title' => $this->t('Link to entity'),
      '#description' => $this->t('Make entity label a link to entity page.'),
93 94 95 96 97 98 99 100 101
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_entity']),
    );
    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
102
  public function render(ResultRow $values) {
103 104
    $type = $this->getValue($values, $this->definition['entity type field']);
    $value = $this->getValue($values);
105

106 107
    if (empty($this->loadedReferencers[$type][$value])) {
      return;
108 109
    }

110
    /** @var $entity \Drupal\Core\Entity\EntityInterface */
111 112
    $entity = $this->loadedReferencers[$type][$value];

113 114
    if (!empty($this->options['link_to_entity'])) {
      $this->options['alter']['make_link'] = TRUE;
115
      $this->options['alter']['url'] = $entity->urlInfo();
116 117 118 119 120 121 122 123 124 125 126 127 128
    }

    return $this->sanitizeValue($entity->label());
  }

  /**
   * {@inheritdoc}
   */
  public function preRender(&$values) {
    parent::preRender($values);

    $entity_ids_per_type = array();
    foreach ($values as $value) {
129 130 131
      if ($type = $this->getValue($value, 'type')) {
        $entity_ids_per_type[$type][] = $this->getValue($value);
      }
132 133 134
    }

    foreach ($entity_ids_per_type as $type => $ids) {
135
      $this->loadedReferencers[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);
136 137 138 139
    }
  }

}