You are here

class TipPluginTextExtended in Tour UI 8

This plugin override Tour\tip\TipPluginText to add UI methods.

It should not appear as tour\tip plugin because tour_ui shouldn't be installed on production. So this plugin will not be availble anymore once the module will be installed. The only goal of this plugin is to provide missing ui methods for default tip text plugin.

Plugin annotation


@Tip(
  id = "text_extended",
  title = @Translation("Text")
)

Hierarchy

Expanded class hierarchy of TipPluginTextExtended

File

src/Plugin/tour_ui/tip/TipPluginTextExtended.php, line 23

Namespace

Drupal\tour_ui\Plugin\tour_ui\tip
View source
class TipPluginTextExtended extends TipPluginText {

  /**
   * {@inheritdoc}
   *
   * TODO: Remove this method when
   * https://www.drupal.org/node/2851166#comment-11925707 will be commited.
   */
  public function getConfiguration() {
    $names = [
      'id',
      'plugin',
      'label',
      'weight',
      'attributes',
      'body',
      'location',
    ];
    foreach ($names as $name) {
      $properties[$name] = $this
        ->get($name);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $id = $this
      ->get('id');
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#required' => TRUE,
      '#default_value' => $this
        ->get('label'),
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => '\\Drupal\\tour\\Entity\\Tour::load',
        'replace_pattern' => '[^a-z0-9-]+',
        'replace' => '-',
      ],
      '#default_value' => $id,
      '#disabled' => !empty($id),
    ];
    $form['plugin'] = [
      '#type' => 'value',
      '#value' => $this
        ->get('plugin'),
    ];
    $form['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#default_value' => $this
        ->get('weight'),
      '#attributes' => [
        'class' => [
          'tip-order-weight',
        ],
      ],
      '#delta' => 100,
    ];
    $attributes = $this
      ->getAttributes();
    $form['attributes'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Attributes'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    ];

    // Determine the type identifier of the tip.
    if (!empty($attributes['data-id'])) {
      $tip_type = 'data-id';
    }
    elseif (!empty($attributes['data-class'])) {
      $tip_type = 'data-class';
    }
    else {
      $tip_type = 'modal';
    }
    $form['attributes']['selector_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Selector type'),
      '#description' => $this
        ->t('The type of selector that this tip will target.'),
      '#options' => [
        'data-id' => $this
          ->t('Data ID'),
        'data-class' => $this
          ->t('Data Class'),
        'modal' => $this
          ->t('Modal'),
      ],
      '#default_value' => $tip_type,
      '#element_validate' => [
        [
          $this,
          'optionsFormValidate',
        ],
      ],
    ];
    $form['attributes']['data-id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Data id'),
      '#description' => $this
        ->t('Provide the ID of the page element.'),
      '#field_prefix' => '#',
      '#default_value' => !empty($attributes['data-id']) ? $attributes['data-id'] : '',
      '#states' => [
        'visible' => [
          'select[name="attributes[selector_type]"]' => [
            'value' => 'data-id',
          ],
        ],
        'enabled' => [
          'select[name="attributes[selector_type]"]' => [
            'value' => 'data-id',
          ],
        ],
      ],
    ];
    $form['attributes']['data-class'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Data class'),
      '#description' => $this
        ->t('Provide the Class of the page element. You can provide more complex jquery selection like <pre>action-links a[href="/admin/structure/forum/add/forum"]</pre>'),
      '#field_prefix' => '.',
      '#default_value' => !empty($attributes['data-class']) ? $attributes['data-class'] : '',
      '#states' => [
        'visible' => [
          'select[name="attributes[selector_type]"]' => [
            'value' => 'data-class',
          ],
        ],
        'enabled' => [
          'select[name="attributes[selector_type]"]' => [
            'value' => 'data-class',
          ],
        ],
      ],
    ];
    $form['location'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Location'),
      '#options' => [
        'top' => $this
          ->t('Top'),
        'bottom' => $this
          ->t('Bottom'),
        'left' => $this
          ->t('Left'),
        'right' => $this
          ->t('Right'),
      ],
      '#default_value' => $this
        ->get('location'),
    ];
    $tags = Xss::getAdminTagList();
    $form['body'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Body'),
      '#required' => TRUE,
      '#default_value' => $this
        ->get('body'),
      '#description' => $this
        ->t('You could use the following tags: %s', [
        '%s' => implode(', ', $tags),
      ]),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Validates the selector_type tip optionsForm().
   *
   * @param mixed $element
   *   The form element that has the validate attached.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The state of the form after submission.
   * @param array $form
   *   The form array.
   */
  public function optionsFormValidate($element, FormStateInterface $form_state, array $form) {
    $selector_type = $form_state
      ->get([
      'attributes',
      'selector_type',
    ]);
    $form_state
      ->unsetValue([
      'attributes',
      'selector_type',
    ]);

    // If selector_type is modal we need to ensure that there is
    // no data-id or data-class specified.
    if ($selector_type == 'modal') {
      $form_state
        ->unsetValue([
        'attributes',
        'data-id',
      ]);
      $form_state
        ->unsetValue([
        'attributes',
        'data-class',
      ]);
    }

    // If data-id was selected and no id provided.
    if ($selector_type == 'data-id' && $form_state
      ->isValueEmpty([
      'attributes',
      'data-id',
    ])) {
      $form_state
        ->setError($form['attributes']['data-id'], $this
        ->t('Please provide a data id.'));
    }

    // If data-class was selected and no class provided.
    if ($selector_type == 'data-class' && $form_state
      ->isValueEmpty([
      'attributes',
      'data-class',
    ])) {
      $form_state
        ->setError($form['attributes']['data-class'], $this
        ->t('Please provide a data class.'));
    }

    // Remove the data-class value if data-id is provided.
    if ($selector_type == 'data-id') {
      $form_state
        ->unsetValue([
        'attributes',
        'data-class',
      ]);
    }

    // Remove the data-id value is data-class is provided.
    if ($selector_type == 'data-class') {
      $form_state
        ->unsetValue([
        'attributes',
        'data-id',
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.
TipPluginBase::$attributes protected property The attributes that will be applied to the markup of this tip.
TipPluginBase::$label protected property The label which is used for render of this tip.
TipPluginBase::$weight protected property Allows tips to take more priority that others.
TipPluginBase::get public function Used for returning values by key. Overrides TipPluginInterface::get
TipPluginBase::getLabel public function Returns label of the tip. Overrides TipPluginInterface::getLabel
TipPluginBase::getWeight public function Returns weight of the tip. Overrides TipPluginInterface::getWeight
TipPluginBase::id public function Returns id of the tip. Overrides TipPluginInterface::id
TipPluginBase::set public function Used for returning values by key. Overrides TipPluginInterface::set
TipPluginText::$ariaId protected property Unique aria-id.
TipPluginText::$body protected property The body text which is used for render of this Text Tip.
TipPluginText::$location protected property The forced position of where the tip will be located.
TipPluginText::$token protected property Token service.
TipPluginText::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TipPluginText::getAriaId public function Returns a ID that is guaranteed uniqueness.
TipPluginText::getAttributes public function Returns an array of attributes for the tip wrapper. Overrides TipPluginBase::getAttributes
TipPluginText::getBody public function Returns body of the text tip.
TipPluginText::getLocation public function Returns location of the text tip.
TipPluginText::getOutput public function Returns a renderable array. Overrides TipPluginInterface::getOutput
TipPluginText::__construct public function Constructs a \Drupal\tour\Plugin\tour\tip\TipPluginText object. Overrides PluginBase::__construct
TipPluginTextExtended::buildConfigurationForm public function
TipPluginTextExtended::getConfiguration public function TODO: Remove this method when https://www.drupal.org/node/2851166#comment-11925707 will be commited.
TipPluginTextExtended::optionsFormValidate public function Validates the selector_type tip optionsForm().
TipPluginTextExtended::submitConfigurationForm public function