View source
<?php
namespace Drupal\blazy\Plugin\Field\FieldFormatter;
abstract class FormatterBase implements FormatterInterface {
protected $admin;
protected $settings = [];
protected $defaultSettingsMerged = FALSE;
protected $htmlSettings = [];
protected $fieldInstance = [];
protected $fieldDefinition = [];
protected $fieldDisplay = [];
protected $pluginId;
protected $langcode;
protected $viewMode;
protected $isMultiple = FALSE;
protected $knownEntities = [
'field_collection',
'paragraphs',
];
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'];
$this->targetType = in_array($field['type'], $this->knownEntities) ? $field['type'] . '_item' : $field['type'];
}
public function getSettings() {
if (!$this->defaultSettingsMerged) {
$this
->mergeDefaults();
}
return $this->settings;
}
public function setSettings(array $settings = []) {
$this->settings = $settings;
$this->defaultSettingsMerged = FALSE;
return $this;
}
public function getSetting($key) {
if (!$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
$this
->mergeDefaults();
}
return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
}
public function setSetting($key, $value) {
$this->settings[$key] = $value;
return $this;
}
protected function mergeDefaults() {
$this->settings += static::defaultSettings();
$this->defaultSettingsMerged = TRUE;
}
public function setHtmlSettings(array $settings = []) {
$this->htmlSettings = $settings;
return $this;
}
public function getHtmlSettings() {
return array_merge((array) $this->htmlSettings, (array) $this
->getSettings());
}
public function getPluginId() {
return $this->pluginId;
}
public function buildSettings() {
return array_merge((array) $this->htmlSettings, (array) $this->settings);
}
public function view($items, $langcode, $entity_type, $entity, $display) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$this->fieldDisplay = $display;
$this->entity = $entity;
$settings = $this
->setupFieldVariables();
$settings['entity_id'] = $entity_id;
$this->langcode = $settings['langcode'] = $langcode;
$this->viewMode = isset($settings['current_view_mode']) ? $settings['current_view_mode'] : 'default';
$this
->setHtmlSettings($settings);
return $this
->viewElements($items, $entity);
}
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());
}
public function buildSettingsSummary($view_mode) {
$this->fieldDisplay = $this->fieldInstance['display'][$view_mode];
$this->viewMode = $view_mode;
$this
->setupFieldVariables();
return $this
->settingsSummary($this
->getScopedFormElements());
}
protected function setupFieldVariables() {
$settings = $this->fieldDisplay['settings'];
$this
->setSettings($settings);
$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;
}
public static function defaultSettings() {
return [];
}
public abstract function viewElements($items, $entity);
public function getRequiredForms() {
return [];
}
public function settingsForm($form, &$form_state, $definition) {
return [];
}
public abstract function settingsSummary(array $definition);
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,
];
}
}