EntityLabel.php in Commerce License 8.2
File
src/Plugin/views/field/EntityLabel.php
View source
<?php
namespace Drupal\commerce_license\Plugin\views\field;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\EntityLabel as CoreEntityLabel;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
class EntityLabel extends CoreEntityLabel {
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
FieldPluginBase::init($view, $display, $options);
if (isset($this->definition['entity type field'])) {
$this->additional_fields[$this->definition['entity type field']] = $this->definition['entity type field'];
}
}
public function preRender(&$values) {
$entity_ids_per_type = [];
foreach ($values as $value) {
$type = $this
->getEntityTypeFromValues($value);
$entity_ids_per_type[$type][] = $this
->getValue($value);
}
foreach ($entity_ids_per_type as $type => $ids) {
if (property_exists($this, 'entityTypeManager')) {
$this->loadedReferencers[$type] = $this->entityTypeManager
->getStorage($type)
->loadMultiple($ids);
}
else {
$this->loadedReferencers[$type] = $this->entityManager
->getStorage($type)
->loadMultiple($ids);
}
}
}
protected function getEntityTypeFromValues(ResultRow $row) {
if (!empty($this->definition['entity type field'])) {
$entity_type_id = $this
->getValue($row, $this->definition['entity type field']);
return $entity_type_id;
}
else {
return parent::getEntityType();
}
}
public function render(ResultRow $values) {
$entity_type_id = $this
->getEntityTypeFromValues($values);
$value = $this
->getValue($values);
if (empty($this->loadedReferencers[$entity_type_id][$value])) {
return;
}
$entity = $this->loadedReferencers[$entity_type_id][$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());
}
}
Classes
Name |
Description |
EntityLabel |
Field handler to display entity label optionally linked to entity page. |