You are here

GridStackFormatterTrait.php in GridStack 8.2

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

File

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

namespace Drupal\gridstack\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A Trait common for gridstack formatters.
 */
trait GridStackFormatterTrait {

  /**
   * The gridstack field formatter manager.
   *
   * @var \Drupal\gridstack\GridStackFormatterInterface
   */
  protected $formatter;

  /**
   * The gridstack field formatter manager.
   *
   * @var \Drupal\gridstack\GridStackManagerInterface
   */
  protected $manager;

  /**
   * The logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * Returns the gridstack field formatter service.
   */
  public function formatter() {
    return $this->formatter;
  }

  /**
   * Returns the gridstack service.
   */
  public function manager() {
    return $this->manager;
  }

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

  /**
   * Injects DI services.
   */
  protected static function injectServices($instance, ContainerInterface $container, $type = '') {
    $instance->formatter = $instance->blazyManager = $container
      ->get('gridstack.formatter');
    $instance->manager = $container
      ->get('gridstack.manager');

    // Blazy:2.x+ might already set these, provides a failsafe.
    if ($type == 'image' || $type == 'entity') {
      $instance->imageFactory = isset($instance->imageFactory) ? $instance->imageFactory : $container
        ->get('image.factory');
      if ($type == 'entity') {
        $instance->loggerFactory = isset($instance->loggerFactory) ? $instance->loggerFactory : $container
          ->get('logger.factory');
        $instance->blazyEntity = isset($instance->blazyEntity) ? $instance->blazyEntity : $container
          ->get('blazy.entity');
        $instance->blazyOembed = isset($instance->blazyOembed) ? $instance->blazyOembed : $instance->blazyEntity
          ->oembed();
      }
    }
    return $instance;
  }

  /**
   * Returns the gridstack admin service shortcut.
   */
  public function admin() {
    return \Drupal::service('gridstack.admin');
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    return $this
      ->admin()
      ->getSettingsSummary($this
      ->getScopedFormElements());
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    return $field_definition
      ->getFieldStorageDefinition()
      ->isMultiple();
  }

  /**
   * Builds the settings.
   */
  public function buildSettings() {
    $settings = array_merge($this
      ->getCommonFieldDefinition(), $this
      ->getSettings());
    $settings['blazy'] = TRUE;
    $settings['third_party'] = $this
      ->getThirdPartySettings();
    return $settings;
  }

  /**
   * Defines the common scope for both front and admin.
   */
  public function getCommonFieldDefinition() {
    $field = $this->fieldDefinition;
    return [
      'namespace' => 'gridstack',
      'current_view_mode' => $this->viewMode,
      'field_name' => $field
        ->getName(),
      'field_type' => $field
        ->getType(),
      'entity_type' => $field
        ->getTargetEntityTypeId(),
      'plugin_id' => $this
        ->getPluginId(),
      'target_type' => $this
        ->getFieldSetting('target_type'),
      'style' => FALSE,
      'grid_form' => FALSE,
    ];
  }

  /**
   * Defines the common scope for the form elements.
   */
  public function getCommonScopedFormElements() {
    return [
      'settings' => $this
        ->getSettings(),
    ] + $this
      ->getCommonFieldDefinition();
  }

}

Traits

Namesort descending Description
GridStackFormatterTrait A Trait common for gridstack formatters.