You are here

abstract class FormEntryTypeBase in Flag 8.4

Base class for link types using form entry.

Hierarchy

Expanded class hierarchy of FormEntryTypeBase

File

src/Plugin/ActionLink/FormEntryTypeBase.php, line 14

Namespace

Drupal\flag\Plugin\ActionLink
View source
abstract class FormEntryTypeBase extends ActionLinkTypeBase implements FormEntryInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $options = parent::defaultConfiguration();
    $options += [
      'flag_confirmation' => $this
        ->t('Flag this content?'),
      'unflag_confirmation' => $this
        ->t('Unflag this content?'),
      'flag_create_button' => $this
        ->t('Create flagging'),
      'flag_delete_button' => $this
        ->t('Delete flagging'),
      'form_behavior' => 'default',
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $plugin_id = $this
      ->getPluginId();
    $plugin_description = $this->pluginDefinition['label'];
    $form['display']['settings']['link_options_' . $plugin_id] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this
        ->t('Options for the "@label" link type', [
        '@label' => $plugin_description,
      ]),
      // Any "link type" provider module must put its settings fields inside
      // a fieldset whose HTML ID is link-options-LINKTYPE, where LINKTYPE is
      // the machine-name of the link type. This is necessary for the
      // radiobutton's JavaScript dependency feature to work.
      '#id' => 'link-options-confirm',
    ];
    $form['display']['settings']['link_options_' . $plugin_id]['flag_confirmation'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Flag confirmation message'),
      '#default_value' => $this
        ->getFlagQuestion(),
      '#description' => $this
        ->t('Message displayed if the user has clicked the "flag this" link and confirmation is required. Usually presented in the form of a question such as, "Are you sure you want to flag this content?"'),
      // This will get changed to a state by flag_link_type_options_states().
      '#required' => TRUE,
    ];
    $form['display']['settings']['link_options_' . $plugin_id]['unflag_confirmation'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Unflag confirmation message'),
      '#default_value' => $this
        ->getUnflagQuestion(),
      '#description' => $this
        ->t('Message displayed if the user has clicked the "unflag this" link and confirmation is required. Usually presented in the form of a question such as, "Are you sure you want to unflag this content?"'),
      // This will get changed to a state by flag_link_type_options_states().
      '#required' => TRUE,
    ];
    $form['display']['settings']['link_options_' . $plugin_id]['flag_create_button'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Create flagging button text'),
      '#default_value' => $this->configuration['flag_create_button'],
      '#description' => $this
        ->t('The text for the submit button when creating a flagging.'),
      // This will get changed to a state by flag_link_type_options_states().
      '#required' => TRUE,
    ];
    $form['display']['settings']['link_options_' . $plugin_id]['flag_delete_button'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Delete flagging button text'),
      '#default_value' => $this->configuration['flag_delete_button'],
      '#description' => $this
        ->t('The text for the submit button when deleting a flagging.'),
      // This will get changed to a state by flag_link_type_options_states().
      '#required' => TRUE,
    ];
    $form['display']['settings']['link_options_' . $plugin_id]['form_behavior'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Form behavior'),
      '#options' => [
        'default' => $this
          ->t('New page'),
        'dialog' => $this
          ->t('Dialog'),
        'modal' => $this
          ->t('Modal dialog'),
      ],
      '#description' => $this
        ->t('If an option other than <em>new page</em> is selected, the form will open via AJAX on the same page.'),
      '#default_value' => $this->configuration['form_behavior'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state
      ->getValues();
    if ($form_values['link_type'] == 'confirm') {
      if (empty($form_values['flag_confirmation'])) {
        $form_state
          ->setErrorByName('flag_confirmation', $this
          ->t('A flag confirmation message is required when using the "@label" link type.', [
          '@label' => $this->pluginDefinition['label'],
        ]));
      }
      if (empty($form_values['unflag_confirmation'])) {
        $form_state
          ->setErrorByName('unflag_confirmation', $this
          ->t('An unflag confirmation message is required when using the "@label" link type.', [
          '@label' => $this->pluginDefinition['label'],
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    foreach (array_keys($this
      ->defaultConfiguration()) as $key) {
      $this->configuration[$key] = $form_state
        ->getValue($key);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAsFlagLink(FlagInterface $flag, EntityInterface $entity) {
    $render = parent::getAsFlagLink($flag, $entity);
    if ($this->configuration['form_behavior'] !== 'default') {
      $render['#attached']['library'][] = 'core/drupal.ajax';
      $render['#attributes']['class'][] = 'use-ajax';
      $render['#attributes']['data-dialog-type'] = $this->configuration['form_behavior'];
      $render['#attributes']['data-dialog-options'] = Json::encode([
        'width' => 'auto',
      ]);
    }
    return $render;
  }

  /**
   * {@inheritdoc}
   */
  public function getFlagQuestion() {
    return $this->configuration['flag_confirmation'];
  }

  /**
   * {@inheritdoc}
   */
  public function getEditFlaggingTitle() {
    return $this->configuration['edit_flagging'];
  }

  /**
   * {@inheritdoc}
   */
  public function getUnflagQuestion() {
    return $this->configuration['unflag_confirmation'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCreateButtonText() {
    return $this->configuration['flag_create_button'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDeleteButtonText() {
    return $this->configuration['flag_delete_button'];
  }

  /**
   * {@inheritdoc}
   */
  public function getUpdateButtonText() {
    return $this->configuration['flag_update_button'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionLinkTypeBase::$currentUser protected property The current user.
ActionLinkTypeBase::calculateDependencies public function
ActionLinkTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ActionLinkTypeBase::getAction protected function Helper method to get the next flag action the user can take.
ActionLinkTypeBase::getAsLink public function Get the action link as a Link object. Overrides ActionLinkTypePluginInterface::getAsLink
ActionLinkTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ActionLinkTypeBase::getDestination protected function Helper method to generate a destination URL parameter. 1
ActionLinkTypeBase::getUrl abstract protected function Return a Url object for the given flag action. 3
ActionLinkTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ActionLinkTypeBase::__construct public function Build a new link type instance and sets the configuration. Overrides PluginBase::__construct 1
FormEntryTypeBase::buildConfigurationForm public function Provides a form array for the action link plugin's settings form. Overrides ActionLinkTypeBase::buildConfigurationForm 1
FormEntryTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ActionLinkTypeBase::defaultConfiguration 1
FormEntryTypeBase::getAsFlagLink public function Get the action link formatted for use in entity links. Overrides ActionLinkTypeBase::getAsFlagLink
FormEntryTypeBase::getCreateButtonText public function Returns the create button text. Overrides FormEntryInterface::getCreateButtonText
FormEntryTypeBase::getDeleteButtonText public function Returns the delete button text. Overrides FormEntryInterface::getDeleteButtonText
FormEntryTypeBase::getEditFlaggingTitle public function Returns the edit flagging details form title. Overrides FormEntryInterface::getEditFlaggingTitle
FormEntryTypeBase::getFlagQuestion public function Returns the flag confirm form question when flagging. Overrides FormEntryInterface::getFlagQuestion
FormEntryTypeBase::getUnflagQuestion public function Returns the flag confirm form question when unflagging. Overrides FormEntryInterface::getUnflagQuestion
FormEntryTypeBase::getUpdateButtonText public function Returns the update button text. Overrides FormEntryInterface::getUpdateButtonText
FormEntryTypeBase::submitConfigurationForm public function Processes the action link setting form submit. Overrides ActionLinkTypeBase::submitConfigurationForm
FormEntryTypeBase::validateConfigurationForm public function Validates the action link setting form. Overrides ActionLinkTypeBase::validateConfigurationForm 1
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.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.