View source
<?php
namespace Drupal\settings_tray\Block;
use Drupal\block\BlockForm;
use Drupal\block\BlockInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Url;
class BlockEntitySettingTrayForm extends BlockForm {
use AjaxFormHelperTrait;
public function title(BlockInterface $block) {
return $this
->t('Configure @block', [
'@block' => $block
->getPlugin()
->getPluginDefinition()['admin_label'],
]);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$query = [];
if ($destination = $this
->getRequest()->query
->get('destination')) {
$query['destination'] = $destination;
}
$form['advanced_link'] = [
'#type' => 'link',
'#title' => $this
->t('Advanced block options'),
'#url' => $this->entity
->toUrl('edit-form', [
'query' => $query,
]),
'#weight' => 1000,
];
unset($form['id'], $form['region'], $form['settings']['admin_label']);
if (isset($form['settings']['label_display']) && isset($form['settings']['label'])) {
$form['settings']['label_display']['#weight'] = -100;
$form['settings']['label']['#states']['visible'] = [
':input[name="settings[label_display]"]' => [
'checked' => TRUE,
],
];
$form['settings']['label']['#title'] = $this
->t("Block title");
$form['settings']['label_display']['#title'] = $this
->t("Display block title");
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save @block', [
'@block' => $this->entity
->getPlugin()
->getPluginDefinition()['admin_label'],
]);
$actions['delete']['#access'] = FALSE;
return $actions;
}
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
return [];
}
protected function validateVisibility(array $form, FormStateInterface $form_state) {
}
protected function submitVisibility(array $form, FormStateInterface $form_state) {
}
protected function getPluginForm(BlockPluginInterface $block) {
if ($block instanceof PluginWithFormsInterface) {
return $this->pluginFormFactory
->createInstance($block, 'settings_tray', 'configure');
}
return $block;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => '::ajaxSubmit',
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
return $form;
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
if ($redirect_url = $this
->getRedirectUrl()) {
$command = new RedirectCommand($redirect_url
->setAbsolute()
->toString());
}
else {
throw new \Exception("No destination provided by Settings Tray form");
}
$response = new AjaxResponse();
return $response
->addCommand($command);
}
protected function getRedirectUrl() {
if ($this
->getRequest()->query
->has('destination') && ($destination = $this
->getRedirectDestination()
->get())) {
return Url::fromUserInput('/' . $destination);
}
}
}