BlazyEntityBase.php in Blazy 7
File
src/Plugin/Field/FieldFormatter/BlazyEntityBase.php
View source
<?php
namespace Drupal\blazy\Plugin\Field\FieldFormatter;
use Drupal\blazy\BlazyEntity;
abstract class BlazyEntityBase extends BlazyFormatterBase {
protected $blazyEntity;
public function blazyEntity() {
if (!isset($this->blazyEntity)) {
$this->blazyEntity = new BlazyEntity($this->formatter);
}
return $this->blazyEntity;
}
protected function getEntitiesToView($items) {
if (empty($items)) {
return [];
}
$entities = [];
foreach ($items as $item) {
if (empty($item['access'])) {
continue;
}
$entity = clone $item['entity'];
unset($entity->content);
$entities[] = $entity;
}
return $entities;
}
public function buildElements(array &$build, $entities) {
$settings =& $build['settings'];
foreach ($entities as $delta => $entity) {
$this->targetType = $settings['target_type'] = $entity->targetType;
list($entity_id) = entity_extract_ids($this->targetType, $entity);
static $depth = 0;
$depth++;
if ($depth > 20) {
throw new \Exception(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', [
'@entity_type' => $settings['entity_type_id'],
'@entity_id' => $entity_id,
]));
}
$settings['delta'] = $delta;
$settings['entity_id'] = $entity_id;
$this
->buildElement($build, $entity, $delta);
$depth = 0;
}
$settings['check_blazy'] = empty($settings['vanilla']);
}
public function buildElement(array &$build, $entity, $delta = 0) {
$build['items'][$delta] = $this
->blazyEntity()
->entityView($entity->targetType, $entity, $build['settings']) ?: [];
}
public function settingsForm($form, &$form_state, $definition) {
parent::settingsForm($form, $form_state, $definition);
$element = [];
$definition['_views'] = isset($form['field_api_classes']);
$this
->admin()
->buildSettingsForm($element, $definition);
return $element;
}
public function getScopedFormElements() {
return [
'use_view_mode' => TRUE,
] + parent::getScopedFormElements();
}
}
Classes
Name |
Description |
BlazyEntityBase |
Base class for entity reference formatters without field details. |