Unverified Commit 9c0c8e95 authored by Stefan Auditor's avatar Stefan Auditor
Browse files

Issue #3273359: Add theme variables as formatter settings

parent 0b6384a8
Loading
Loading
Loading
Loading
+1 −17
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\clipboardjs\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'clipboard' formatter.
 *
@@ -24,20 +22,6 @@ use Drupal\Core\Field\FieldItemListInterface;
 */
class ClipboardJs extends ClipboardJsBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    // phpcs:ignore
    @trigger_error('ClipboardJs formatter is deprecated in clipboardjs:2.0.2 and is removed from clipboardjs:2.1.0. Instead, use one of the other available formatters of this project. See https://git.drupalcode.org/project/clipboardjs/-/tree/2.0.x/src/Plugin/Field/FieldFormatter', E_USER_DEPRECATED);
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'clipboardjs',
        '#value' => $this->viewValue($item),
      ];
    }
    return $elements;
  }
  const TEMPLATE = 'clipboardjs';

}
+50 −5
Original line number Diff line number Diff line
@@ -13,20 +13,51 @@ use Drupal\Core\Form\FormStateInterface;
 */
class ClipboardJsBase extends FormatterBase {

  const TEMPLATE = 'clipboardjs';

  const ALERT_STYLES = ['tooltip', 'alert', 'none'];

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [] + parent::defaultSettings();
    return [
      'label' => 'Click to copy',
      'alert_style' => 'tooltip',
      'alert_text' => 'Copied!',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    return [
      // Implement settings form.
    ] + parent::settingsForm($form, $form_state);
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#description' => $this->t('The label on the button or hovertip.'),
      '#default_value' => $this->getSetting('label'),
    ];
    $form['alert_style'] = [
      '#type' => 'select',
      '#title' => $this->t('Alert style'),
      '#description' => $this->t('The alert style e.g. <em>Tooltip</em>, <em>Alert</em> or <em>None</em>.'),
      '#default_value' => $this->getSetting('alert_style'),
      '#options' => array_combine(
        static::ALERT_STYLES,
        array_map(
          'ucfirst',
          static::ALERT_STYLES
        )
      ),
    ];
    $form['alert_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Alert text'),
      '#description' => $this->t('The alert text, shown as tooltip or alert.'),
      '#default_value' => $this->getSetting('alert_text'),
    ];
    return $form + parent::settingsForm($form, $form_state);
  }

  /**
@@ -34,6 +65,15 @@ class ClipboardJsBase extends FormatterBase {
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = $this->t('Label: @label', [
      '@label' => $this->getSetting('label'),
    ]);
    $summary[] = $this->t('Alert style: @alert_style', [
      '@alert_style' => $this->getSetting('alert_style'),
    ]);
    $summary[] = $this->t('Alert text: @alert_text', [
      '@alert_text' => $this->getSetting('alert_text'),
    ]);
    return $summary;
  }

@@ -44,8 +84,13 @@ class ClipboardJsBase extends FormatterBase {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'clipboardjs_button',
        '#theme' => static::TEMPLATE,
        // @codingStandardsIgnoreStart
        '#label' => $this->t($this->getSetting('label')),
        '#value' => $this->viewValue($item),
        '#alert_style' => $this->getSetting('alert_style'),
        '#alert_text' => $this->t($this->getSetting('alert_text')),
        // @codingStandardsIgnoreEnd
      ];
    }
    return $elements;
+1 −15
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\clipboardjs\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'clipboard' formatter.
 *
@@ -19,18 +17,6 @@ use Drupal\Core\Field\FieldItemListInterface;
 */
class ClipboardJsButton extends ClipboardJsBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'clipboardjs_button',
        '#value' => $this->viewValue($item),
      ];
    }
    return $elements;
  }
  const TEMPLATE = 'clipboardjs_button';

}
+1 −15
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\clipboardjs\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'clipboard' formatter.
 *
@@ -20,18 +18,6 @@ use Drupal\Core\Field\FieldItemListInterface;
 */
class ClipboardJsSnippet extends ClipboardJsBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'clipboardjs_snippet',
        '#value' => $this->viewValue($item),
      ];
    }
    return $elements;
  }
  const TEMPLATE = 'clipboardjs_snippet';

}
+1 −15
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\clipboardjs\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'clipboard' formatter.
 *
@@ -20,18 +18,6 @@ use Drupal\Core\Field\FieldItemListInterface;
 */
class ClipboardJsTextarea extends ClipboardJsBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'clipboardjs_textarea',
        '#value' => $this->viewValue($item),
      ];
    }
    return $elements;
  }
  const TEMPLATE = 'clipboardjs_textarea';

}
Loading