You are here

abstract class BlazyEntityReferenceBase in Blazy 7

Base class for entity reference formatters with field details.

Applicable for Field Collection, Paragraphs, etc, save for File Entity.

Hierarchy

Expanded class hierarchy of BlazyEntityReferenceBase

See also

Drupal\slick\Plugin\Field\FieldFormatter\SlickFieldCollectionFormatter

Drupal\slick\Plugin\Field\FieldFormatter\SlickParagraphsFormatter

File

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

Namespace

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

  /**
   * The link options.
   *
   * @var object
   */
  protected $linkOptions = [];

  /**
   * The string options.
   *
   * @var object
   */
  protected $stringOptions = [];

  /**
   * The text options.
   *
   * @var object
   */
  protected $textOptions = [];

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return BlazyDefault::extendedSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function buildSettings() {
    return [
      'blazy' => TRUE,
      'ratio' => 'fluid',
    ] + parent::buildSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function getRequiredForms() {
    return [
      'fieldable' => TRUE,
    ] + parent::getRequiredForms();
  }

  /**
   * {@inheritdoc}
   */
  public function buildElement(array &$build, $entity, $delta = 0) {
    $settings =& $build['settings'];
    $item_id = $settings['item_id'] = empty($settings['item_id']) ? 'box' : $settings['item_id'];
    if (!empty($settings['vanilla'])) {
      return parent::buildElement($build, $entity);
    }
    $element = [
      'settings' => $settings,
    ];

    // Built early before stage to allow custom highres video thumbnail later.
    // Implementor must import Drupal\blazy\Dejavu\BlazyVideoTrait.
    // Build the main stage.
    if (!empty($settings['image'])) {
      if (method_exists($this, 'getMediaItem') && isset($entity
        ->wrapper()->{$settings['image']})) {
        $file = (object) $entity
          ->wrapper()->{$settings['image']}
          ->value();
        if ($file && ($image = $this
          ->getImageItem($file))) {
          $element['item'] = $image['item'];
          $element['settings'] = array_merge($settings, $image['settings']);
        }
        $this
          ->getMediaItem($element, $file);
      }

      // If Image rendered is picked, render image as is, a rare case.
      if (!empty($settings['media_switch']) && $settings['media_switch'] == 'rendered') {
        $content = $this
          ->blazyEntity()
          ->getFieldRenderable($entity, $settings['image'], $settings, TRUE);
      }
    }

    // Optional image with responsive image, lazyLoad, and lightbox supports.
    $element[$item_id] = isset($content) ? $content : $this
      ->formatter()
      ->getBlazy($element);

    // Captions if so configured.
    $this
      ->getCaption($element, $entity);

    // Layouts can be builtin, or field, if so configured.
    if (!empty($settings['layout']) && isset($entity->{$settings['layout']})) {
      if (strpos($settings['layout'], 'field_') !== FALSE) {
        $settings['layout'] = $this
          ->blazyEntity()
          ->getFieldString($entity, $settings['layout'], $settings);
      }
      $element['settings']['layout'] = $settings['layout'];
    }

    // Classes, if so configured.
    if (!empty($settings['class']) && isset($entity->{$settings['class']})) {
      $element['settings']['class'] = $this
        ->blazyEntity()
        ->getFieldString($entity, $settings['class'], $settings);
    }

    // Build the main item.
    $build['items'][$delta] = $element;

    // Build the thumbnail item.
    if (!empty($settings['nav'])) {

      // Thumbnail usages: asNavFor pagers, dot, arrows, photobox thumbnails.
      $element[$item_id] = empty($settings['thumbnail_style']) ? [] : $this
        ->formatter()
        ->getThumbnail($element['settings'], $element['item']);
      $element['caption'] = empty($settings['thumbnail_caption']) ? [] : $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $settings['thumbnail_caption'], $settings);
      $build['thumb']['items'][$delta] = $element;
    }
  }

  /**
   * Builds slide captions with possible multi-value fields.
   */
  public function getCaption(array &$element, $entity) {
    $settings = $element['settings'];

    // Title can be plain text, or link field.
    if (!empty($settings['title']) && isset($entity->{$settings['title']})) {
      if ($title = $this
        ->blazyEntity()
        ->getFieldTextOrLink($entity, $settings['title'], $settings)) {
        $element['caption']['title'] = $title;
      }
    }

    // Other caption fields, if so configured.
    if (!empty($settings['caption'])) {
      $caption_items = $weights = [];
      foreach ($settings['caption'] as $i => $field_caption) {
        if (!isset($entity->{$field_caption})) {
          continue;
        }
        if ($caption = $this
          ->blazyEntity()
          ->getFieldRenderable($entity, $field_caption, $settings)) {
          if (isset($caption['#weight'])) {
            $weights[] = $caption['#weight'];
          }
          $caption_items[$i] = $caption;
        }
      }
      if ($caption_items) {
        if ($weights) {
          array_multisort($weights, SORT_ASC, $caption_items);
        }
        $element['caption']['data'] = $caption_items;
      }
    }

    // Link, if so configured.
    if (!empty($settings['link']) && isset($entity->{$settings['link']})) {
      $element['caption']['link'] = $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $settings['link'], $settings);
    }
    if (!empty($settings['overlay']) && isset($entity->{$settings['overlay']})) {
      $element['caption']['overlay'] = $this
        ->blazyEntity()
        ->getFieldRenderable($entity, $settings['overlay'], $settings);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm($form, &$form_state, $definition) {
    $element = parent::settingsForm($form, $form_state, $definition);
    if (isset($element['layout'])) {
      $layout_description = $element['layout']['#description'];
      $element['layout']['#description'] = t('Create a dedicated List (text - max number 1) field related to the caption placement to have unique layout per slide with the following supported keys: top, right, bottom, left, center, center-top, etc. Be sure its formatter is Key.') . ' ' . $layout_description;
    }
    if (isset($element['media_switch'])) {
      $element['media_switch']['#options']['rendered'] = t('Image rendered by its formatter');
      $element['media_switch']['#description'] .= ' ' . t('Be sure the enabled fields here are not hidden/disabled at its view mode.');
    }
    if (isset($element['caption'])) {
      $element['caption']['#description'] = t('Check fields to be treated as captions, even if not caption texts.');
    }
    if (isset($element['image']['#description'])) {
      $element['image']['#description'] .= ' ' . t('For video, this allows separate highres image, be sure the same field used for Image to have a mix of videos and images. Leave empty to fallback to the video provider thumbnails. The formatter/renderer is managed by <strong>@namespace</strong> formatter. Meaning original formatter ignored. If you want original formatters, check <strong>Vanilla</strong> option. Alternatively choose <strong>Media switcher &gt; Image rendered </strong>, other image-related settings here will be ignored. <strong>Supported fields</strong>: Image, Video Embed Field.', [
        '@namespace' => $this
          ->getPluginId(),
      ]);
    }
    if (isset($element['overlay']['#description'])) {
      $element['overlay']['#description'] .= ' ' . t('The formatter/renderer is managed by the child formatter. <strong>Supported fields</strong>: Image, File/ Media Entity.');
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function getScopedFormElements() {
    $bundles = $this->fieldDefinition['bundles'];
    $links = [
      'text',
      'link_field',
      'url',
    ];
    $strings = [
      'text',
      'list_text',
    ];
    $texts = [
      'text',
      'text_long',
      'text_with_summary',
      'link_field',
      'url',
    ];
    $this->linkOptions = $this
      ->admin()
      ->getFieldOptions($this->fieldInstance, $links, $this->targetType, $bundles);
    $this->stringOptions = $this
      ->admin()
      ->getFieldOptions($this->fieldInstance, $strings, $this->targetType, $bundles);
    $this->textOptions = $this
      ->admin()
      ->getFieldOptions($this->fieldInstance, $texts, $this->targetType, $bundles);
    return [
      'multimedia' => TRUE,
      'vanilla' => TRUE,
      'captions' => $this
        ->admin()
        ->getFieldOptions($this->fieldInstance, [], $this->targetType, $bundles),
      'classes' => $this->stringOptions,
      'images' => $this
        ->admin()
        ->getFieldOptions($this->fieldInstance, [
        'image',
      ], $this->targetType, $bundles),
      'layouts' => $this->stringOptions,
      'links' => $this->linkOptions,
      'titles' => $this->textOptions,
      '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::buildElements public function Returns media contents.
BlazyEntityBase::getEntitiesToView protected function Converts $items array to object for easy D8 -> D7 backports. Overrides BlazyFormatterBase::getEntitiesToView
BlazyEntityReferenceBase::$linkOptions protected property The link options.
BlazyEntityReferenceBase::$stringOptions protected property The string options.
BlazyEntityReferenceBase::$textOptions protected property The text options.
BlazyEntityReferenceBase::buildElement public function Build individual item contents. Overrides BlazyEntityBase::buildElement
BlazyEntityReferenceBase::buildSettings public function Builds the settings. Overrides FormatterBase::buildSettings
BlazyEntityReferenceBase::defaultSettings public static function Returns default settings. Overrides BlazyFormatterBase::defaultSettings
BlazyEntityReferenceBase::getCaption public function Builds slide captions with possible multi-value fields.
BlazyEntityReferenceBase::getRequiredForms public function Returns required form elements for the current formatter. Overrides BlazyFormatterBase::getRequiredForms
BlazyEntityReferenceBase::getScopedFormElements public function Defines the scope for the form elements. Overrides BlazyEntityBase::getScopedFormElements
BlazyEntityReferenceBase::settingsForm public function Implements hook_field_formatter_settings_form(). Overrides BlazyEntityBase::settingsForm
BlazyFormatterBase::admin abstract public function Returns the blazy admin service for blazy-related module. 1
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::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