You are here

trait ElementClassTrait in Element Class Formatter 8

Trait ElementClassTrait.

@package Drupal\element_class_formatter\Plugin\Field\FieldFormatter

Hierarchy

File

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

Namespace

Drupal\element_class_formatter\Plugin\Field\FieldFormatter
View source
trait ElementClassTrait {
  use StringTranslationTrait;

  /**
   * Default class value.
   *
   * @param array $settings
   *   The original default settings array.
   *
   * @return array
   *   The new default settings array.
   */
  public static function elementClassDefaultSettings(array $settings) {
    return [
      'class' => '',
    ] + $settings;
  }

  /**
   * Setting form to collect class value.
   *
   * @param array $elements
   *   The original elements render array.
   * @param string $class
   *   The class string.
   *
   * @return array
   *   The updated elements render array.
   */
  public function elementClassSettingsForm(array $elements, $class) {
    $elements['class'] = [
      '#type' => 'textfield',
      '#default_value' => $class,
      '#title' => $this
        ->t('Element class'),
      '#description' => 'A space separated set of classes.',
      '#maxlength' => 200,
    ];
    return $elements;
  }

  /**
   * Text for settings summary.
   *
   * @param array $summary
   *   The original summary array.
   * @param string $class
   *   The class string.
   *
   * @return array
   *   The updated summary array.
   */
  public function elementClassSettingsSummary(array $summary, $class) {
    if ($class) {
      $summary[] = $this
        ->t('Element class: @class', [
        '@class' => $class,
      ]);
    }
    return $summary;
  }

  /**
   * Set the class on the element.
   *
   * @param array $elements
   *   The original elements render array.
   * @param string $class
   *   The class string.
   * @param \Drupal\Core\Field\FieldItemListInterface $items
   *   The list of field items.
   *
   * @return array
   *   The updated elements render array.
   */
  public function setElementClass(array $elements, $class, FieldItemListInterface $items) {
    foreach ($items as $delta => $item) {

      // Add class.
      if (!empty($class)) {
        $elements[$delta]['#options']['attributes']['class'][] = $class;
      }
    }
    return $elements;
  }

  /**
   * Set the class on the entity.
   *
   * @param array $elements
   *   The original elements render array.
   * @param string $class
   *   The class string.
   * @param \Drupal\Core\Entity\EntityInterface[] $entities
   *   The referenced entities.
   *
   * @return array
   *   The updated elements render array.
   */
  public function setEntityClass(array $elements, $class, array $entities) {
    foreach ($entities as $delta => $entity) {

      // Add class.
      if (!empty($class)) {
        $elements[$delta]['#item_attributes']['class'][] = $class;
      }
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ElementClassTrait::elementClassDefaultSettings public static function Default class value.
ElementClassTrait::elementClassSettingsForm public function Setting form to collect class value.
ElementClassTrait::elementClassSettingsSummary public function Text for settings summary.
ElementClassTrait::setElementClass public function Set the class on the element.
ElementClassTrait::setEntityClass public function Set the class on the entity.
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.