Unverified Commit fffe0a25 authored by rikki_iki's avatar rikki_iki Committed by GitHub
Browse files

Merge pull request #1 from rikki-iki/refactor

Refactor to make more reusable
parents e1ffaa77 97d67cb9
......@@ -6,7 +6,7 @@ use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Trait ElementWithClass.
* Trait ElementClassTrait.
*
* @package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
*/
......
<?php
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Trait ElementEntityClassTrait.
*
* @package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
*/
trait ElementEntityClassTrait {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$entities = $this->getEntitiesToView($items, $langcode);
$class = $this->getSetting('class');
return $this->setEntityClass($elements, $class, $entities);
}
}
<?php
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Trait ElementLinkClassTrait.
*
* @package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
*/
trait ElementLinkClassTrait {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$class = $this->getSetting('class');
return $this->setElementClass($elements, $class, $items);
}
}
<?php
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
/**
* Trait ElementListClassTrait.
*
* @package Drupal\element_class_formatter\Plugin\Field\FieldFormatter
*/
trait ElementListClassTrait {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
$elements['list_type'] = [
'#title' => $this->t('List type'),
'#type' => 'select',
'#options' => [
'ul' => 'Un-ordered list',
'ol' => 'Ordered list',
],
'#default_value' => $this->getSetting('list_type'),
];
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
if ($tag = $this->getSetting('list_type')) {
$summary[] = $this->t('List type: @tag', ['@tag' => $tag]);
}
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$class = $this->getSetting('class');
$attributes = new Attribute();
if (!empty($class)) {
$attributes->addClass($class);
}
return [
[
'#theme' => 'item_list',
'#items' => $elements,
'#list_type' => $this->getSetting('list_type'),
'#attributes' => $attributes->toArray(),
],
];
}
}
......@@ -2,8 +2,6 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\MailToFormatter;
/**
......@@ -19,43 +17,6 @@ use Drupal\Core\Field\Plugin\Field\FieldFormatter\MailToFormatter;
*/
class EmailLinkClassFormatter extends MailToFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$class = $this->getSetting('class');
return $this->setElementClass($elements, $class, $items);
}
use ElementLinkClassTrait;
}
......@@ -2,10 +2,7 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Template\Attribute;
/**
* Plugin implementation of the 'file with class' formatter.
......@@ -20,71 +17,6 @@ use Drupal\Core\Template\Attribute;
*/
class EntityReferenceListLabelClassFormatter extends EntityReferenceLabelFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
$elements['list_type'] = [
'#title' => $this->t('List type'),
'#type' => 'select',
'#options' => [
'ul' => 'Un-ordered list',
'ol' => 'Ordered list',
],
'#default_value' => $this->getSetting('list_type'),
];
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
if ($tag = $this->getSetting('list_type')) {
$summary[] = $this->t('List type: @tag', ['@tag' => $tag]);
}
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$attributes = new Attribute();
$class = $this->getSetting('class');
if (!empty($class)) {
$attributes->addClass($class);
}
return [
[
'#theme' => 'item_list',
'#items' => $elements,
'#list_type' => $this->getSetting('list_type'),
'#attributes' => $attributes->toArray(),
],
];
}
use ElementListClassTrait;
}
......@@ -2,8 +2,6 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
/**
......@@ -19,44 +17,6 @@ use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
*/
class ImageClassFormatter extends ImageFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$entities = $this->getEntitiesToView($items, $langcode);
$class = $this->getSetting('class');
return $this->setEntityClass($elements, $class, $entities);
}
use ElementEntityClassTrait;
}
......@@ -2,8 +2,6 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
/**
......@@ -19,43 +17,6 @@ use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
*/
class LinkClassFormatter extends LinkFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$class = $this->getSetting('class');
return $this->setElementClass($elements, $class, $items);
}
use ElementLinkClassTrait;
}
......@@ -2,10 +2,7 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
use Drupal\Core\Template\Attribute;
/**
* Formatter for displaying links in an HTML list.
......@@ -20,72 +17,6 @@ use Drupal\Core\Template\Attribute;
*/
class LinkListClassFormatter extends LinkFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$default_settings = parent::defaultSettings() + [
'list_type' => 'ul',
];
return ElementClassTrait::elementClassDefaultSettings($default_settings);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
$elements['list_type'] = [
'#title' => $this->t('List type'),
'#type' => 'select',
'#options' => [
'ul' => 'Un-ordered list',
'ol' => 'Ordered list',
],
'#default_value' => $this->getSetting('list_type'),
];
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
if ($tag = $this->getSetting('list_type')) {
$summary[] = $this->t('List type: @tag', ['@tag' => $tag]);
}
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$class = $this->getSetting('class');
$attributes = new Attribute();
if (!empty($class)) {
$attributes->addClass($class);
}
return [
[
'#theme' => 'item_list',
'#items' => $elements,
'#list_type' => $this->getSetting('list_type'),
'#attributes' => $attributes->toArray(),
],
];
}
use ElementListClassTrait;
}
......@@ -2,8 +2,6 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter;
/**
......@@ -19,44 +17,6 @@ use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter
*/
class ResponsiveImageClassFormatter extends ResponsiveImageFormatter {
use ElementClassTrait;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return ElementClassTrait::elementClassDefaultSettings(parent::defaultSettings());
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$class = $this->getSetting('class');
return $this->elementClassSettingsForm($elements, $class);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$class = $this->getSetting('class');
return $this->elementClassSettingsSummary($summary, $class);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$entities = $this->getEntitiesToView($items, $langcode);
$class = $this->getSetting('class');
return $this->setEntityClass($elements, $class, $entities);
}
use ElementEntityClassTrait;
}
......@@ -2,8 +2,6 @@
namespace Drupal\element_class_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;