You are here

class DeleteStyles in Block Style Plugins 8.2

Provides a form to delete a block style.

@internal

Hierarchy

Expanded class hierarchy of DeleteStyles

1 string reference to 'DeleteStyles'
block_style_plugins.routing.yml in ./block_style_plugins.routing.yml
block_style_plugins.routing.yml

File

src/Form/DeleteStyles.php, line 23

Namespace

Drupal\block_style_plugins\Form
View source
class DeleteStyles extends ConfirmFormBase {
  use AjaxFormHelperTrait;
  use LayoutBuilderHighlightTrait;
  use LayoutRebuildTrait;

  /**
   * The layout tempstore repository.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   */
  protected $layoutTempstoreRepository;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The section storage.
   *
   * @var \Drupal\layout_builder\SectionStorageInterface
   */
  protected $sectionStorage;

  /**
   * The layout section delta.
   *
   * @var int
   */
  protected $delta;

  /**
   * The uuid of the block component.
   *
   * @var string
   */
  protected $uuid;

  /**
   * The plugin id of the style to be removed.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * Constructs a DeleteStyles object.
   *
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The layout tempstore repository.
   */
  public function __construct(FormBuilderInterface $form_builder, LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
    $this->formBuilder = $form_builder;
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('form_builder'), $container
      ->get('layout_builder.tempstore_repository'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $uuid = NULL, $plugin_id = NULL) {
    $this->sectionStorage = $section_storage;
    $this->delta = $delta;
    $this->uuid = $uuid;
    $this->pluginId = $plugin_id;
    $form = parent::buildForm($form, $form_state);
    $form['actions']['cancel'] = $this
      ->buildCancelLink();
    $form['#attributes']['data-layout-builder-target-highlight-id'] = $this
      ->blockUpdateHighlightId($this->uuid);
    if ($this
      ->isAjax()) {
      $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to delete this style?');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    $parameters = $this
      ->getParameters();
    return new Url('block_style_plugins.layout_builder.styles', $parameters);
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'block_style_plugins_layout_builder_delete_styles';
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $component = $this->sectionStorage
      ->getSection($this->delta)
      ->getComponent($this->uuid);
    $component
      ->unsetThirdPartySetting('block_style_plugins', $this->pluginId);
    $this->layoutTempstoreRepository
      ->set($this->sectionStorage);
    $form_state
      ->setRedirectUrl($this->sectionStorage
      ->getLayoutBuilderUrl());
  }

  /**
   * {@inheritdoc}
   */
  protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
    return $this
      ->rebuildAndClose($this->sectionStorage);
  }

  /**
   * Build a cancel button for the confirm form.
   */
  protected function buildCancelLink() {
    return [
      '#type' => 'button',
      '#value' => $this
        ->getCancelText(),
      '#ajax' => [
        'callback' => '::ajaxCancel',
      ],
    ];
  }

  /**
   * Provides an ajax callback for the cancel button.
   */
  public function ajaxCancel(array &$form, FormStateInterface $form_state) {
    $parameters = $this
      ->getParameters();
    $new_form = $this->formBuilder
      ->getForm(BlockStyleForm::class, $this->sectionStorage, $parameters['delta'], $parameters['uuid']);
    $new_form['#action'] = $this
      ->getCancelUrl()
      ->toString();
    $response = new AjaxResponse();
    $response
      ->addCommand(new OpenOffCanvasDialogCommand($this
      ->t('Configure Styles'), $new_form));
    return $response;
  }

  /**
   * Gets the parameters needed for the various Url() and form invocations.
   *
   * @return array
   *   List of Url parameters.
   */
  protected function getParameters() {
    return [
      'section_storage_type' => $this->sectionStorage
        ->getStorageType(),
      'section_storage' => $this->sectionStorage
        ->getStorageId(),
      'delta' => $this->delta,
      'uuid' => $this->uuid,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxFormHelperTrait::ajaxSubmit public function Submit form dialog #ajax callback.
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 1
ConfirmFormBase::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText 20
ConfirmFormBase::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription 11
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DeleteStyles::$delta protected property The layout section delta.
DeleteStyles::$formBuilder protected property The form builder.
DeleteStyles::$layoutTempstoreRepository protected property The layout tempstore repository.
DeleteStyles::$pluginId protected property The plugin id of the style to be removed.
DeleteStyles::$sectionStorage protected property The section storage.
DeleteStyles::$uuid protected property The uuid of the block component.
DeleteStyles::ajaxCancel public function Provides an ajax callback for the cancel button.
DeleteStyles::buildCancelLink protected function Build a cancel button for the confirm form.
DeleteStyles::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
DeleteStyles::create public static function Instantiates a new instance of this class. Overrides FormBase::create
DeleteStyles::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
DeleteStyles::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DeleteStyles::getParameters protected function Gets the parameters needed for the various Url() and form invocations.
DeleteStyles::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
DeleteStyles::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DeleteStyles::successfulAjaxSubmit protected function Allows the form to respond to a successful AJAX submission. Overrides AjaxFormHelperTrait::successfulAjaxSubmit
DeleteStyles::__construct public function Constructs a DeleteStyles object.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LayoutBuilderHighlightTrait::blockAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::blockUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutRebuildTrait::rebuildAndClose protected function Rebuilds the layout.
LayoutRebuildTrait::rebuildLayout protected function Rebuilds the layout.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.