BlazyEntityBase.php in Blazy 8.2
File
src/Dejavu/BlazyEntityBase.php
View source
<?php
namespace Drupal\blazy\Dejavu;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
abstract class BlazyEntityBase extends EntityReferenceFormatterBase {
public function buildElements(array &$build, $entities, $langcode) {
foreach ($entities as $delta => $entity) {
static $depth = 0;
$depth++;
if ($depth > 20) {
$this->loggerFactory
->get('entity')
->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', [
'@entity_type' => $entity
->getEntityTypeId(),
'@entity_id' => $entity
->id(),
]);
return $build;
}
$build['settings']['delta'] = $delta;
$build['settings']['langcode'] = $langcode;
$this
->buildElement($build, $entity, $langcode);
$this
->formatter()
->getRenderer()
->addCacheableDependency($build['items'][$delta], $entity);
$depth = 0;
}
}
public function buildElement(array &$build, $entity, $langcode) {
$view_mode = empty($build['settings']['view_mode']) ? 'full' : $build['settings']['view_mode'];
$delta = $build['settings']['delta'];
$build['items'][$delta] = $this
->formatter()
->getEntityTypeManager()
->getViewBuilder($entity
->getEntityTypeId())
->view($entity, $view_mode, $langcode);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$definition = $this
->getScopedFormElements();
$definition['_views'] = isset($form['field_api_classes']);
$this
->admin()
->buildSettingsForm($element, $definition);
return $element;
}
public function buildSettings() {
$settings = array_merge($this
->getCommonFieldDefinition(), $this
->getSettings());
$settings['third_party'] = $this
->getThirdPartySettings();
return $settings;
}
public function getCommonFieldDefinition() {
$field = $this->fieldDefinition;
return [
'current_view_mode' => $this->viewMode,
'field_name' => $field
->getName(),
'field_type' => $field
->getType(),
'entity_type' => $field
->getTargetEntityTypeId(),
'plugin_id' => $this
->getPluginId(),
'target_type' => $this
->getFieldSetting('target_type'),
];
}
public function getScopedFormElements() {
$views_ui = $this
->getFieldSetting('handler') == 'default';
$bundles = $views_ui ? [] : $this
->getFieldSetting('handler_settings')['target_bundles'];
return [
'settings' => $this
->getSettings(),
'target_bundles' => $bundles,
'view_mode' => $this->viewMode,
] + $this
->getCommonFieldDefinition();
}
}
Classes
Name |
Description |
BlazyEntityBase |
Base class for entity reference formatters without field details. |