You are here

class SlickMediaFormatter in Slick Media 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/SlickMediaFormatter.php \Drupal\slick_media\Plugin\Field\FieldFormatter\SlickMediaFormatter
  2. 8 src/Plugin/Field/FieldFormatter/SlickMediaFormatter.php \Drupal\slick_media\Plugin\Field\FieldFormatter\SlickMediaFormatter

Plugin implementation of the 'slick media entity' formatter.

Plugin annotation


@FieldFormatter(
  id = "slick_media",
  label = @Translation("Slick Media"),
  description = @Translation("Display the referenced entities as a Slick carousel."),
  field_types = {
    "entity_reference"
  },
  quickedit = {
    "editor" = "disabled"
  }
)

Hierarchy

  • class \Drupal\slick_media\Plugin\Field\FieldFormatter\SlickMediaFormatter extends \Drupal\slick\Plugin\Field\FieldFormatter\SlickEntityReferenceFormatterBase uses \Drupal\blazy\Dejavu\BlazyVideoTrait

Expanded class hierarchy of SlickMediaFormatter

File

src/Plugin/Field/FieldFormatter/SlickMediaFormatter.php, line 26

Namespace

Drupal\slick_media\Plugin\Field\FieldFormatter
View source
class SlickMediaFormatter extends SlickEntityReferenceFormatterBase {
  use BlazyVideoTrait;

  /**
   * Returns the blazy manager.
   */
  public function blazyManager() {
    return $this->formatter;
  }

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

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $entities = $this
      ->getEntitiesToView($items, $langcode);

    // Early opt-out if the field is empty.
    if (empty($entities)) {
      return [];
    }

    // Collects specific settings to this formatter.
    $settings = $this
      ->getSettings();

    // Asks for Blazy to deal with iFrames, and mobile-optimized lazy loading.
    $settings['blazy'] = TRUE;
    $settings['plugin_id'] = $this
      ->getPluginId();

    // Sets dimensions once to reduce method ::transformDimensions() calls.
    // @todo: A more flexible way to also support paragraphs at one go.
    $entities = array_values($entities);
    if (!empty($settings['image_style']) && $entities[0]
      ->getEntityTypeId() == 'media') {
      $fields = $entities[0]
        ->getFields();
      if (isset($fields['thumbnail'])) {
        $item = $fields['thumbnail']
          ->get(0);
        $settings['item'] = $item;
        $settings['uri'] = $item->entity
          ->getFileUri();
      }
    }
    $build = [
      'settings' => $settings,
    ];
    $this->formatter
      ->buildSettings($build, $items);

    // Build the elements.
    $this
      ->buildElements($build, $entities, $langcode);
    return $this
      ->manager()
      ->build($build);
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    $storage = $field_definition
      ->getFieldStorageDefinition();
    return $storage
      ->isMultiple() && $storage
      ->getSetting('target_type') === 'media';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SlickMediaFormatter::blazyManager public function Returns the blazy manager.
SlickMediaFormatter::defaultSettings public static function
SlickMediaFormatter::isApplicable public static function
SlickMediaFormatter::viewElements public function