You are here

GridStackMediaFormatter.php in GridStack 8.2

Same filename and directory in other branches
  1. 8 src/Plugin/Field/FieldFormatter/GridStackMediaFormatter.php

File

src/Plugin/Field/FieldFormatter/GridStackMediaFormatter.php
View source
<?php

namespace Drupal\gridstack\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\blazy\Plugin\Field\FieldFormatter\BlazyMediaFormatterBase;
use Drupal\gridstack\GridStackDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'GridStack Media' formatter.
 *
 * @FieldFormatter(
 *   id = "gridstack_media",
 *   label = @Translation("GridStack Media"),
 *   description = @Translation("Display the core Media as a GridStack."),
 *   field_types = {"entity_reference"},
 *   quickedit = {"editor" = "disabled"}
 * )
 */
class GridStackMediaFormatter extends BlazyMediaFormatterBase {
  use GridStackFormatterTrait;
  use GridStackFormatterViewTrait;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    return self::injectServices($instance, $container, 'entity');
  }

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

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

    // Early opt-out if the field is empty.
    if (empty($entities)) {
      return [];
    }
    return $this
      ->commonViewElements($items, $langcode, $entities);
  }

  /**
   * {@inheritdoc}
   */
  public function getScopedFormElements() {
    return [
      'vanilla' => TRUE,
      'no_ratio' => TRUE,
      'responsive_image' => TRUE,
    ] + $this
      ->getCommonScopedFormElements() + parent::getScopedFormElements();
  }

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

}

Classes

Namesort descending Description
GridStackMediaFormatter Plugin implementation of the 'GridStack Media' formatter.