You are here

abstract class GridStackEntityFormatterBase in GridStack 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/GridStackEntityFormatterBase.php \Drupal\gridstack\Plugin\Field\FieldFormatter\GridStackEntityFormatterBase

Base class for gridstack entity reference formatters without field details.

Hierarchy

Expanded class hierarchy of GridStackEntityFormatterBase

File

src/Plugin/Field/FieldFormatter/GridStackEntityFormatterBase.php, line 13

Namespace

Drupal\gridstack\Plugin\Field\FieldFormatter
View source
abstract class GridStackEntityFormatterBase extends BlazyEntityBase {
  use GridStackFormatterTrait {
    buildSettings as traitBuildSettings;
  }
  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() {
    $settings = GridStackDefault::baseSettings();
    $settings['view_mode'] = '';
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function buildElement(array &$build, $entity, $langcode) {
    $view_mode = empty($build['settings']['view_mode']) ? 'full' : $build['settings']['view_mode'];
    $delta = $build['settings']['delta'];

    // Adds element box to contain any entity as expected by GridStackManager.
    $build['items'][$delta] = [
      'box' => $this
        ->formatter()
        ->getEntityTypeManager()
        ->getViewBuilder($entity
        ->getEntityTypeId())
        ->view($entity, $view_mode, $langcode),
    ];
  }

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

  /**
   * Builds the settings.
   */
  public function buildSettings() {
    return [
      'vanilla' => TRUE,
    ] + $this
      ->traitBuildSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function getScopedFormElements() {
    return $this
      ->getCommonScopedFormElements() + parent::getScopedFormElements();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackEntityFormatterBase::buildElement public function
GridStackEntityFormatterBase::buildSettings public function Builds the settings.
GridStackEntityFormatterBase::create public static function
GridStackEntityFormatterBase::defaultSettings public static function
GridStackEntityFormatterBase::getScopedFormElements public function
GridStackEntityFormatterBase::viewElements public function
GridStackFormatterTrait::$formatter protected property The gridstack field formatter manager.
GridStackFormatterTrait::$loggerFactory protected property The logger factory.
GridStackFormatterTrait::$manager protected property The gridstack field formatter manager.
GridStackFormatterTrait::admin public function Returns the gridstack admin service shortcut.
GridStackFormatterTrait::blazyManager public function Overrides the blazy manager.
GridStackFormatterTrait::buildSettings public function Builds the settings. Aliased as: traitBuildSettings
GridStackFormatterTrait::formatter public function Returns the gridstack field formatter service.
GridStackFormatterTrait::getCommonFieldDefinition public function Defines the common scope for both front and admin.
GridStackFormatterTrait::getCommonScopedFormElements public function Defines the common scope for the form elements.
GridStackFormatterTrait::injectServices protected static function Injects DI services.
GridStackFormatterTrait::isApplicable public static function 4
GridStackFormatterTrait::manager public function Returns the gridstack service.
GridStackFormatterTrait::settingsSummary public function
GridStackFormatterViewTrait::commonViewElements public function Returns similar view elements.