View source
<?php
namespace Drupal\flag\Plugin\ActionLink;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\flag\ActionLink\ActionLinkTypeBase;
use Drupal\flag\FlagInterface;
abstract class FormEntryTypeBase extends ActionLinkTypeBase implements FormEntryInterface {
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;
}
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,
]),
'#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?"'),
'#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?"'),
'#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.'),
'#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.'),
'#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;
}
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'],
]));
}
}
}
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);
}
}
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;
}
public function getFlagQuestion() {
return $this->configuration['flag_confirmation'];
}
public function getEditFlaggingTitle() {
return $this->configuration['edit_flagging'];
}
public function getUnflagQuestion() {
return $this->configuration['unflag_confirmation'];
}
public function getCreateButtonText() {
return $this->configuration['flag_create_button'];
}
public function getDeleteButtonText() {
return $this->configuration['flag_delete_button'];
}
public function getUpdateButtonText() {
return $this->configuration['flag_update_button'];
}
}