You are here

class FFTFormatter in Field Formatter Template 8.2

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

Plugin implementation of the 'fft_formatter' formatter.

Plugin annotation


@FieldFormatter(
  id = "fft_formatter",
  label = @Translation("Formatter Template"),
  field_types = {
    "text",
    "text_long",
    "text_with_summary",
  },
)

Hierarchy

Expanded class hierarchy of FFTFormatter

File

src/Plugin/Field/FieldFormatter/FFTFormatter.php, line 22

Namespace

Drupal\fft\Plugin\Field\FieldFormatter
View source
class FFTFormatter extends EntityReferenceFormatterBase {
  const FFT_PREPROCESS = 'fft.preprocess';

  /**
   * Check is reference type.
   *
   * @param string $type
   *   The type of field.
   *
   * @return bool
   *   Return value.
   */
  private static function isReference($type) {
    return substr($type, 0, 16) === 'entity_reference';
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'template' => '',
      'image_style_1' => '',
      'image_style_2' => '',
      'settings' => '',
      'reset' => 1,
      'isNew' => 1,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);
    $settings = $this
      ->getSettings();
    $field['type'] = $this->fieldDefinition
      ->getType();
    $fft_templates = fft_get_templates();
    $options_set = $fft_templates['templates'];
    $form['#attached']['library'][] = 'fft/backend';
    if ($settings['isNew'] == 0) {
      $fft_templates['settings'][$settings['template']] = $settings['settings'];
    }
    $form['#attached']['drupalSettings']['fft'] = !empty($fft_templates['settings']) ? $fft_templates['settings'] : [];
    $form['isNew'] = [
      '#type' => 'hidden',
      '#value' => 0,
    ];
    $form['template'] = [
      '#title' => $this
        ->t('Template'),
      '#type' => 'select',
      '#options' => $options_set,
      '#default_value' => $settings['template'],
      '#attributes' => [
        'class' => [
          'fft-template',
        ],
      ],
    ];
    $form['reset'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Reset'),
      '#options' => [
        0 => 'No',
        1 => 'Yes',
      ],
      '#default_value' => $settings['reset'],
      '#description' => $this
        ->t('Reset default Drupal field wrapper markup.'),
    ];
    switch ($field['type']) {
      case 'image':
        $image_style_options = image_style_options();
        $form['image_style_1'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Image Styles 1'),
          '#options' => $image_style_options,
          '#default_value' => $settings['image_style_1'],
        ];
        $form['image_style_2'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Image Styles 2'),
          '#options' => $image_style_options,
          '#default_value' => $settings['image_style_2'],
        ];
        break;
    }
    $settings_des[] = $this
      ->t('Add settings extras for template, one setting per line with syntax key = value.');
    $settings_des[] = $this
      ->t('Support array like key[] = value or key[name] = value.');
    $form['settings'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Settings Extras'),
      '#default_value' => '',
      '#attributes' => [
        'class' => [
          'fft-settings',
        ],
      ],
    ];
    $form['settings_des'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('More Information'),
      '#open' => FALSE,
    ];
    $form['settings_des']['info'] = [
      '#type' => 'markup',
      '#markup' => nl2br(implode("\r\n", $settings_des)),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = $this
      ->t('Formatter Template');
    if ($this
      ->getSetting('template') != '') {
      $fft_template = fft_get_templates('fft');
      if (!empty($fft_template['templates'])) {
        foreach ($fft_template['templates'] as $name => $title) {
          if ($this
            ->getSetting('template') == $name) {
            $summary = [];
            $summary[] = $this
              ->t('Formatter Template: @template', [
              '@template' => $title,
            ]);
            break;
          }
        }
      }
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   *
   * Loads the entities referenced in that field across all the entities being
   * viewed.
   */
  public function prepareView(array $entities_items) {
    $field_type = $this->fieldDefinition
      ->getType();
    if (!$this
      ->isReference($field_type)) {
      return;
    }
    parent::prepareView($entities_items);
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $entity = $items
      ->getEntity();
    $field_type = $this->fieldDefinition
      ->getType();
    $settings = $this
      ->getSettings();
    $data_items = $items;
    if ($this
      ->isReference($field_type)) {
      $data_items = $this
        ->getEntitiesToView($items, $langcode);
    }
    $elements = fft_field_formatter_render($entity, $field_type, $data_items, $settings, $langcode);
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function view(FieldItemListInterface $items, $langcode = NULL) {
    $view = parent::view($items, $langcode);
    if ($this->settings['reset'] === '1') {
      unset($view['#theme']);
    }
    return $view;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityReferenceFormatterBase::checkAccess protected function Checks access to the given entity. 3
EntityReferenceFormatterBase::getEntitiesToView protected function Returns the referenced entities for display. 1
EntityReferenceFormatterBase::needsEntityLoad protected function Returns whether the entity referenced by an item needs to be loaded. 1
FFTFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
FFTFormatter::FFT_PREPROCESS constant
FFTFormatter::isReference private static function Check is reference type.
FFTFormatter::prepareView public function Loads the entities referenced in that field across all the entities being viewed. Overrides EntityReferenceFormatterBase::prepareView
FFTFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
FFTFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
FFTFormatter::view public function Builds a renderable array for a fully themed field. Overrides EntityReferenceFormatterBase::view
FFTFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 11
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct 11
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.