You are here

class SlickTextFormatter in Slick Carousel 7.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/SlickTextFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickTextFormatter
  2. 8 src/Plugin/Field/FieldFormatter/SlickTextFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickTextFormatter

Plugin implementation of the 'Slick Text' formatter.

Hierarchy

Expanded class hierarchy of SlickTextFormatter

File

src/Plugin/Field/FieldFormatter/SlickTextFormatter.php, line 13

Namespace

Drupal\slick\Plugin\Field\FieldFormatter
View source
class SlickTextFormatter extends FormatterBase {
  use SlickFormatterTrait;

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

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

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

  /**
   * {@inheritdoc}
   */
  public function viewElements($items, $entity) {
    $settings = $this
      ->buildSettings();

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

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

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

    // Build the elements.
    $this
      ->buildElements($build, $items);

    // If using 0, or directly passed like D8, taken over by theme_field().
    $element = $this
      ->manager()
      ->build($build);
    return $element;
  }

  /**
   * Build the slick carousel elements.
   */
  public function buildElements(array &$build, $items) {
    $settings = $build['settings'];

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

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

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

  /**
   * Defines the scope for the form elements.
   */
  public function getScopedFormElements() {
    return [
      '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
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().
SlickFormatterTrait::admin public function Returns the slick admin service.
SlickTextFormatter::buildElements public function Build the slick carousel elements.
SlickTextFormatter::defaultSettings public static function Returns default settings. Overrides FormatterBase::defaultSettings
SlickTextFormatter::getRequiredForms public function Returns required form elements for the current formatter. Overrides FormatterBase::getRequiredForms
SlickTextFormatter::getScopedFormElements public function Defines the scope for the form elements. Overrides FormatterBase::getScopedFormElements
SlickTextFormatter::settingsForm public function Implements hook_field_formatter_settings_form(). Overrides FormatterBase::settingsForm
SlickTextFormatter::viewElements public function Implements hook_field_formatter_view(). Overrides SlickFormatterTrait::viewElements
SlickTextFormatter::__construct public function Constructs a SlickTextFormatter instance. Overrides FormatterBase::__construct