You are here

class DeleteComponentForm in Layout Paragraphs 2.0.x

Class DeleteComponentForm.

Hierarchy

Expanded class hierarchy of DeleteComponentForm

1 string reference to 'DeleteComponentForm'
layout_paragraphs.routing.yml in ./layout_paragraphs.routing.yml
layout_paragraphs.routing.yml

File

src/Form/DeleteComponentForm.php, line 19

Namespace

Drupal\layout_paragraphs\Form
View source
class DeleteComponentForm extends FormBase {
  use LayoutParagraphsLayoutRefreshTrait;
  use DialogHelperTrait;

  /**
   * The layout paragraphs layout tempstore.
   *
   * @var \Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository
   */
  protected $tempstore;

  /**
   * The uuid of the component to delete.
   *
   * @var string
   */
  protected $componentUuid;

  /**
   * {@inheritDoc}
   */
  protected function __construct($tempstore) {
    $this->tempstore = $tempstore;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('layout_paragraphs.tempstore_repository'));
  }

  /**
   * {@inheritDoc}
   */
  public function getFormId() {
    return 'layout_paragraphs_delete_component_form';
  }

  /**
   * {@inheritDoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, LayoutParagraphsLayout $layout_paragraphs_layout = NULL, string $component_uuid = NULL) {
    $this
      ->setLayoutParagraphsLayout($layout_paragraphs_layout);
    $this->componentUuid = $component_uuid;
    $component = $this->layoutParagraphsLayout
      ->getComponentByUuid($this->componentUuid);
    $type = $component
      ->getEntity()
      ->getParagraphType()
      ->label();
    $form['#title'] = $this
      ->t('Delete @type', [
      '@type' => $type,
    ]);
    $form['confirm'] = [
      '#markup' => $this
        ->t('Really delete this @type? There is no undo.', [
        '@type' => $type,
      ]),
    ];
    $form['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Delete'),
        '#ajax' => [
          'callback' => '::deleteComponent',
        ],
      ],
      'cancel' => [
        '#type' => 'button',
        '#value' => $this
          ->t('Cancel'),
        '#ajax' => [
          'callback' => '::closeForm',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritDoc}
   *
   * Deletes the component and saves the layout.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->layoutParagraphsLayout
      ->deleteComponent($this->componentUuid, TRUE);
    $this->tempstore
      ->set($this->layoutParagraphsLayout);
  }

  /**
   * Ajax callback - deletes component and closes the form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   */
  public function deleteComponent(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response
      ->addCommand(new CloseDialogCommand($this
      ->dialogSelector($this->layoutParagraphsLayout)));
    if ($this
      ->needsRefresh()) {
      return $this
        ->refreshLayout($response);
    }
    $response
      ->addCommand(new RemoveCommand('[data-uuid="' . $this->componentUuid . '"]'));
    $response
      ->addCommand(new LayoutParagraphsEventCommand($this->layoutParagraphsLayout, $this->componentUuid, 'component:delete'));
    return $response;
  }

  /**
   * Ajax callback - closes the form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   */
  public function closeForm(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $response
      ->addCommand($this
      ->closeDialogCommand($this->layoutParagraphsLayout));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeleteComponentForm::$componentUuid protected property The uuid of the component to delete.
DeleteComponentForm::$tempstore protected property The layout paragraphs layout tempstore.
DeleteComponentForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
DeleteComponentForm::closeForm public function Ajax callback - closes the form.
DeleteComponentForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
DeleteComponentForm::deleteComponent public function Ajax callback - deletes component and closes the form.
DeleteComponentForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DeleteComponentForm::submitForm public function Deletes the component and saves the layout. Overrides FormInterface::submitForm
DeleteComponentForm::__construct protected function
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
DialogHelperTrait::closeDialogCommand protected function Returns a CloseDialogComand with the correct selector.
DialogHelperTrait::dialogId protected function Generates a dialog id for a given layout.
DialogHelperTrait::dialogSelector protected function Generates a dialog selector for a given layout.
DialogHelperTrait::dialogSettings protected function Returns an array of dialog settings for modal edit forms.
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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 72
LayoutParagraphsLayoutRefreshTrait::$eventDispatcher protected property The event dispatcher service. 1
LayoutParagraphsLayoutRefreshTrait::$layoutParagraphsLayout protected property The layout paragraphs layout object.
LayoutParagraphsLayoutRefreshTrait::$originalLayoutParagraphsLayout protected property The original paragraphs layout object.
LayoutParagraphsLayoutRefreshTrait::eventDispatcher protected function Returns the event dispatcher service. 1
LayoutParagraphsLayoutRefreshTrait::needsRefresh protected function Returns TRUE if the layout needs to be refreshed.
LayoutParagraphsLayoutRefreshTrait::refreshLayout protected function Decorates an ajax response with a command to refresh an entire layout.
LayoutParagraphsLayoutRefreshTrait::renderLayout protected function Renders the layout builder UI render array.
LayoutParagraphsLayoutRefreshTrait::setLayoutParagraphsLayout protected function Setter for layoutParagraphsLayout property.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.