You are here

class EntityLabel in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/EntityLabel.php \Drupal\views\Plugin\views\field\EntityLabel
  2. 9 core/modules/views/src/Plugin/views/field/EntityLabel.php \Drupal\views\Plugin\views\field\EntityLabel

Field handler to display entity label optionally linked to entity page.

Plugin annotation

@ViewsField("entity_label");

Hierarchy

  • class \Drupal\views\Plugin\views\field\EntityLabel extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of EntityLabel

File

core/modules/views/src/Plugin/views/field/EntityLabel.php, line 19

Namespace

Drupal\views\Plugin\views\field
View source
class EntityLabel extends FieldPluginBase {

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

  /**
   * EntityTypeManager class.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs an 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.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'));
  }

  /**
   * {@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'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['link_to_entity'] = [
      '#title' => $this
        ->t('Link to entity'),
      '#description' => $this
        ->t('Make entity label a link to entity page.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_entity']),
    ];
    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $type = $this
      ->getValue($values, $this->definition['entity type field']);
    $value = $this
      ->getValue($values);
    if (empty($this->loadedReferencers[$type][$value])) {
      return;
    }

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->loadedReferencers[$type][$value];
    if (!empty($this->options['link_to_entity'])) {
      try {
        $this->options['alter']['url'] = $entity
          ->toUrl();
        $this->options['alter']['make_link'] = TRUE;
      } catch (UndefinedLinkTemplateException $e) {
        $this->options['alter']['make_link'] = FALSE;
      } catch (EntityMalformedException $e) {
        $this->options['alter']['make_link'] = FALSE;
      }
    }
    return $this
      ->sanitizeValue($entity
      ->label());
  }

  /**
   * {@inheritdoc}
   */
  public function preRender(&$values) {
    parent::preRender($values);
    $entity_ids_per_type = [];
    foreach ($values as $value) {
      if ($type = $this
        ->getValue($value, 'type')) {
        $entity_ids_per_type[$type][] = $this
          ->getValue($value);
      }
    }
    foreach ($entity_ids_per_type as $type => $ids) {
      $this->loadedReferencers[$type] = $this->entityTypeManager
        ->getStorage($type)
        ->loadMultiple($ids);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityLabel::$entityTypeManager protected property EntityTypeManager class.
EntityLabel::$loadedReferencers protected property Array of entities that reference to file.
EntityLabel::buildOptionsForm public function
EntityLabel::create public static function
EntityLabel::defineOptions protected function
EntityLabel::init public function
EntityLabel::preRender public function
EntityLabel::render public function
EntityLabel::__construct public function Constructs an EntityLabel object.