You are here

class BlazyTextFormatter in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter

Plugin implementation of the 'Blazy Text' formatter to have a grid of texts.

Hierarchy

Expanded class hierarchy of BlazyTextFormatter

File

src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php, line 12

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter
View source
class BlazyTextFormatter extends FormatterBase {
  use BlazyFormatterTrait;

  /**
   * Constructs a BlazyTextFormatter instance.
   */
  public function __construct($plugin_id, $field, $instance, BlazyManagerInterface $formatter) {
    parent::__construct($plugin_id, $field, $instance);
    $this->formatter = $formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return BlazyDefault::baseSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function getRequiredForms() {
    return [
      'grid' => $this->isMultiple,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function admin() {
    if (!isset($this->admin)) {
      $this->admin = new BlazyAdminFormatter($this->formatter);
    }
    return $this->admin;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements($items, $entity) {
    $settings = $this
      ->buildSettings();
    $settings['_grid'] = !empty($settings['style']) && !empty($settings['grid']);

    // Hand over to default formatter if not multiple as it is for grid only.
    // @todo figure out to disable formatter like isApplicable() at D8 in the
    // first place, and remove this.
    if (!$this->isMultiple || !$settings['_grid']) {
      $types = field_info_field_types($this->fieldDefinition['type']);
      $display['type'] = isset($types['default_formatter']) ? $types['default_formatter'] : '';
      if ($fallback = text_field_formatter_view($this->entityType, $entity, $this->fieldDefinition, $this->fieldInstance, $this->langcode, $items, $display)) {
        return $fallback;
      }
    }
    $settings['vanilla'] = TRUE;
    $settings['namespace'] = $settings['item_id'] = 'blazy';

    // Build the settings.
    $build = [
      'settings' => $settings,
    ];

    // Modifies settings before building elements.
    $this->formatter
      ->preBuildElements($build, $items, $entity);

    // The ProcessedText element already handles cache context & tag bubbling.
    // @see \Drupal\filter\Element\ProcessedText::preRenderText()
    foreach ($items as $item) {
      $content = _text_sanitize($this->fieldInstance, $settings['langcode'], $item, 'value');
      $build[] = [
        '#markup' => $content,
      ];
      unset($content);
    }

    // Pass to manager for easy updates to all Blazy formatters.
    return $this->formatter
      ->build($build);
  }

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

    // @todo remove once D8-like isApplicable() for cardinality - 1 landed.
    if (!$this->isMultiple) {
      return $element;
    }
    $this
      ->admin()
      ->buildSettingsForm($element, $definition);
    return $element;
  }

  /**
   * Defines the scope for the form elements.
   */
  public function getScopedFormElements() {
    return [
      'grid_required' => $this->isMultiple,
      'no_image_style' => TRUE,
      'no_layouts' => TRUE,
      'responsive_image' => FALSE,
      'style' => TRUE,
    ] + parent::getScopedFormElements();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyFormatterTrait::$formatter protected property The blazy-related formatter service.
BlazyFormatterTrait::$manager protected property The blazy field formatter manager.
BlazyFormatterTrait::formatter public function Returns the blazy-related formatter.
BlazyFormatterTrait::manager public function Returns the blazy service.
BlazyFormatterTrait::settingsSummary public function
BlazyTextFormatter::admin public function
BlazyTextFormatter::defaultSettings public static function Returns default settings. Overrides FormatterBase::defaultSettings
BlazyTextFormatter::getRequiredForms public function Returns required form elements for the current formatter. Overrides FormatterBase::getRequiredForms
BlazyTextFormatter::getScopedFormElements public function Defines the scope for the form elements. Overrides FormatterBase::getScopedFormElements
BlazyTextFormatter::settingsForm public function Implements hook_field_formatter_settings_form(). Overrides FormatterBase::settingsForm
BlazyTextFormatter::viewElements public function Implements hook_field_formatter_view(). Overrides FormatterBase::viewElements
BlazyTextFormatter::__construct public function Constructs a BlazyTextFormatter instance. Overrides FormatterBase::__construct
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::getHtmlSettings public function Gets html settings.
FormatterBase::getPluginId public function Gets formatter plugin id.
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::setupFieldVariables protected function Setup common variables across different hooks.
FormatterBase::view public function Implements hook_field_formatter_view().