You are here

abstract class FormatterBase in Blazy 7

Base class for blazy/slick image, and file formatters.

Hierarchy

Expanded class hierarchy of FormatterBase

File

src/Plugin/Field/FieldFormatter/FormatterBase.php, line 8

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter
View source
abstract class FormatterBase implements FormatterInterface {

  /**
   * The blazy-related admin formatter service.
   *
   * @var object
   */
  protected $admin;

  /**
   * The formatter settings.
   *
   * @var array
   */
  protected $settings = [];

  /**
   * Whether default settings have been merged into the current $settings.
   *
   * @var bool
   */
  protected $defaultSettingsMerged = FALSE;

  /**
   * The form settings.
   *
   * @var array
   */
  protected $htmlSettings = [];

  /**
   * The field instance.
   *
   * @var array
   */
  protected $fieldInstance = [];

  /**
   * The field definition.
   *
   * @var array
   */
  protected $fieldDefinition = [];

  /**
   * The field display.
   *
   * @var array
   */
  protected $fieldDisplay = [];

  /**
   * The blazy formatter plugin id.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * The langcode.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The view mode.
   *
   * @var string
   */
  protected $viewMode;

  /**
   * True if the field cardinality equals -1.
   *
   * @var bool
   */
  protected $isMultiple = FALSE;

  /**
   * The known hard-coded entities.
   *
   * @var array
   */
  protected $knownEntities = [
    'field_collection',
    'paragraphs',
  ];

  /**
   * Constructs a base formatter object.
   */
  public function __construct($plugin_id, $field, $instance) {
    $this->pluginId = $plugin_id;
    $this->fieldDefinition = $field;
    $this->fieldInstance = $instance;
    $this->isMultiple = $field['cardinality'] == -1;
    $this->bundle = $instance['bundle'];
    $this->fieldName = $instance['field_name'];
    $this->entityType = $instance['entity_type'];
    $this->fieldType = $field['type'];

    // For more entities, it is overriden via blazy_entity_load().
    // Below is just for few known entities to save plugins from overriding.
    $this->targetType = in_array($field['type'], $this->knownEntities) ? $field['type'] . '_item' : $field['type'];
  }

  /**
   * Gets formatter settings.
   */
  public function getSettings() {

    // Merge defaults before returning the array.
    if (!$this->defaultSettingsMerged) {
      $this
        ->mergeDefaults();
    }
    return $this->settings;
  }

  /**
   * Sets formatter settings.
   */
  public function setSettings(array $settings = []) {
    $this->settings = $settings;
    $this->defaultSettingsMerged = FALSE;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getSetting($key) {

    // Merge defaults if we have no value for the key.
    if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
      $this
        ->mergeDefaults();
    }
    return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setSetting($key, $value) {
    $this->settings[$key] = $value;
    return $this;
  }

  /**
   * Merges default settings values into $settings.
   */
  protected function mergeDefaults() {
    $this->settings += static::defaultSettings();
    $this->defaultSettingsMerged = TRUE;
  }

  /**
   * Sets html settings.
   */
  public function setHtmlSettings(array $settings = []) {
    $this->htmlSettings = $settings;
    return $this;
  }

  /**
   * Gets html settings.
   */
  public function getHtmlSettings() {
    return array_merge((array) $this->htmlSettings, (array) $this
      ->getSettings());
  }

  /**
   * Gets formatter plugin id.
   */
  public function getPluginId() {
    return $this->pluginId;
  }

  /**
   * Builds the settings.
   */
  public function buildSettings() {
    return array_merge((array) $this->htmlSettings, (array) $this->settings);
  }

  /**
   * Implements hook_field_formatter_view().
   */
  public function view($items, $langcode, $entity_type, $entity, $display) {
    list($entity_id) = entity_extract_ids($entity_type, $entity);

    // Simplify relevant function arguments into settings array.
    $this->fieldDisplay = $display;
    $this->entity = $entity;
    $settings = $this
      ->setupFieldVariables();
    $settings['entity_id'] = $entity_id;
    $this->langcode = $settings['langcode'] = $langcode;

    // Gets view_mode from formatter_view.
    $this->viewMode = isset($settings['current_view_mode']) ? $settings['current_view_mode'] : 'default';
    $this
      ->setHtmlSettings($settings);
    return $this
      ->viewElements($items, $entity);
  }

  /**
   * Implements hook_field_formatter_settings_form().
   */
  public function buildSettingsForm($form, &$form_state, $view_mode) {
    $display = $this->fieldInstance['display'][$view_mode];
    $this->fieldDisplay = $display;
    $this->viewMode = $view_mode;
    $this->fieldInstance['display'][$view_mode]['current_view_mode'] = $view_mode;
    $this
      ->setupFieldVariables();
    return $this
      ->settingsForm($form, $form_state, $this
      ->getScopedFormElements());
  }

  /**
   * {@inheritdoc}
   */
  public function buildSettingsSummary($view_mode) {
    $this->fieldDisplay = $this->fieldInstance['display'][$view_mode];
    $this->viewMode = $view_mode;
    $this
      ->setupFieldVariables();
    return $this
      ->settingsSummary($this
      ->getScopedFormElements());
  }

  /**
   * Setup common variables across different hooks.
   */
  protected function setupFieldVariables() {
    $settings = $this->fieldDisplay['settings'];

    // The actual formatter settings for database, and summaries.
    $this
      ->setSettings($settings);

    // Additional settings for the formatters and forms to work with.
    $settings['bundle'] = $this->bundle;
    $settings['field_name'] = $this->fieldName;
    $settings['entity_type_id'] = $this->entityType;
    $settings['field_type'] = $this->fieldType;
    $settings['multiple'] = $this->isMultiple;
    $settings['target_type'] = $this->targetType;
    $settings['plugin_id'] = $this->fieldDisplay['type'];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public abstract function viewElements($items, $entity);

  /**
   * {@inheritdoc}
   */
  public function getRequiredForms() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm($form, &$form_state, $definition) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public abstract function settingsSummary(array $definition);

  /**
   * Defines the scope for the form elements.
   */
  public function getScopedFormElements() {
    return [
      'forms' => $this
        ->getRequiredForms(),
      'entity_type_id' => $this->entityType,
      'field_name' => $this->fieldName,
      'field_type' => $this->fieldType,
      'instance' => $this->fieldInstance,
      'plugin_id' => $this->pluginId,
      'settings' => $this
        ->getSettings(),
      'target_type' => $this->targetType,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::buildSettings public function Builds the settings. 1
FormatterBase::buildSettingsForm public function Implements hook_field_formatter_settings_form().
FormatterBase::buildSettingsSummary public function
FormatterBase::defaultSettings public static function Returns default settings. Overrides FormatterInterface::defaultSettings 2
FormatterBase::getHtmlSettings public function Gets html settings.
FormatterBase::getPluginId public function Gets formatter plugin id.
FormatterBase::getRequiredForms public function Returns required form elements for the current formatter. Overrides FormatterInterface::getRequiredForms 2
FormatterBase::getScopedFormElements public function Defines the scope for the form elements. 2
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::settingsForm public function Implements hook_field_formatter_settings_form(). Overrides FormatterInterface::settingsForm 2
FormatterBase::settingsSummary abstract public function Implements hook_field_formatter_settings_summary(). Overrides FormatterInterface::settingsSummary 1
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