You are here

abstract class BlazyEntityBase in Blazy 7

Base class for entity reference formatters without field details.

Hierarchy

Expanded class hierarchy of BlazyEntityBase

See also

Drupal\slick\Plugin\Field\FieldFormatter\SlickEntityFormatterBase

Drupal\slick\Plugin\Field\FieldFormatter\SlickEntityReferenceFormatterBase

Drupal\slick\Plugin\Field\FieldFormatter\SlickFieldCollectionFormatter

Drupal\slick\Plugin\Field\FieldFormatter\SlickParagraphsFormatter

File

src/Plugin/Field/FieldFormatter/BlazyEntityBase.php, line 15

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter
View source
abstract class BlazyEntityBase extends BlazyFormatterBase {

  /**
   * The blazy entity instance.
   *
   * @var object
   */
  protected $blazyEntity;

  /**
   * Returns the blazy entity object.
   */
  public function blazyEntity() {
    if (!isset($this->blazyEntity)) {
      $this->blazyEntity = new BlazyEntity($this->formatter);
    }
    return $this->blazyEntity;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntitiesToView($items) {
    if (empty($items)) {
      return [];
    }

    // Assumes we have entityreference, or sub-moduled which prepare the view.
    $entities = [];
    foreach ($items as $item) {

      // Skip an item that is not accessible.
      if (empty($item['access'])) {
        continue;
      }
      $entity = clone $item['entity'];
      unset($entity->content);
      $entities[] = $entity;
    }
    return $entities;
  }

  /**
   * Returns media contents.
   */
  public function buildElements(array &$build, $entities) {
    $settings =& $build['settings'];
    foreach ($entities as $delta => $entity) {

      // Overrides Constructor::targetType via blazy_entity_load().
      // Safe as a formatter is designed to work for a particular entity type
      // like what entityreference does, to not be confused with bundles.
      $this->targetType = $settings['target_type'] = $entity->targetType;
      list($entity_id) = entity_extract_ids($this->targetType, $entity);

      // Protect ourselves from recursive rendering.
      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;
    }

    // Supports Blazy formatter multi-breakpoint images if available.
    $settings['check_blazy'] = empty($settings['vanilla']);
  }

  /**
   * Build individual item contents.
   */
  public function buildElement(array &$build, $entity, $delta = 0) {
    $build['items'][$delta] = $this
      ->blazyEntity()
      ->entityView($entity->targetType, $entity, $build['settings']) ?: [];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Defines the scope for the form elements.
   */
  public function getScopedFormElements() {
    return [
      'use_view_mode' => TRUE,
    ] + parent::getScopedFormElements();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyEntityBase::$blazyEntity protected property The blazy entity instance.
BlazyEntityBase::blazyEntity public function Returns the blazy entity object.
BlazyEntityBase::buildElement public function Build individual item contents. Overrides BlazyFormatterBase::buildElement 1
BlazyEntityBase::buildElements public function Returns media contents.
BlazyEntityBase::getEntitiesToView protected function Converts $items array to object for easy D8 -> D7 backports. Overrides BlazyFormatterBase::getEntitiesToView
BlazyEntityBase::getScopedFormElements public function Defines the scope for the form elements. Overrides BlazyFormatterBase::getScopedFormElements 1
BlazyEntityBase::settingsForm public function Implements hook_field_formatter_settings_form(). Overrides BlazyFormatterBase::settingsForm 1
BlazyFormatterBase::admin abstract public function Returns the blazy admin service for blazy-related module. 1
BlazyFormatterBase::defaultSettings public static function Returns default settings. Overrides FormatterBase::defaultSettings 1
BlazyFormatterBase::getRequiredForms public function Returns required form elements for the current formatter. Overrides FormatterBase::getRequiredForms 2
BlazyFormatterBase::settingsSummary public function Implements hook_field_formatter_settings_summary(). Overrides FormatterBase::settingsSummary
FormatterBase::$admin protected property The blazy-related admin formatter service.
FormatterBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$fieldDisplay protected property The field display.
FormatterBase::$fieldInstance protected property The field instance.
FormatterBase::$htmlSettings protected property The form settings.
FormatterBase::$isMultiple protected property True if the field cardinality equals -1.
FormatterBase::$knownEntities protected property The known hard-coded entities.
FormatterBase::$langcode protected property The langcode.
FormatterBase::$pluginId protected property The blazy formatter plugin id.
FormatterBase::$settings protected property The formatter settings.
FormatterBase::$viewMode protected property The view mode.
FormatterBase::buildSettings public function Builds the settings. 1
FormatterBase::buildSettingsForm public function Implements hook_field_formatter_settings_form().
FormatterBase::buildSettingsSummary public function
FormatterBase::getHtmlSettings public function Gets html settings.
FormatterBase::getPluginId public function Gets formatter plugin id.
FormatterBase::getSetting public function
FormatterBase::getSettings public function Gets formatter settings.
FormatterBase::mergeDefaults protected function Merges default settings values into $settings.
FormatterBase::setHtmlSettings public function Sets html settings.
FormatterBase::setSetting public function
FormatterBase::setSettings public function Sets formatter settings.
FormatterBase::setupFieldVariables protected function Setup common variables across different hooks.
FormatterBase::view public function Implements hook_field_formatter_view().
FormatterBase::viewElements abstract public function Implements hook_field_formatter_view(). Overrides FormatterInterface::viewElements 2
FormatterBase::__construct public function Constructs a base formatter object. 2