You are here

class LayoutParagraphsBuilderForm in Layout Paragraphs 2.0.x

Class LayoutParagraphsBuilderForm.

Builds a Layout Paragraphs Builder form with save / cancel buttons for saving the host entity.

Hierarchy

Expanded class hierarchy of LayoutParagraphsBuilderForm

File

src/Form/LayoutParagraphsBuilderForm.php, line 21

Namespace

Drupal\layout_paragraphs\Form
View source
class LayoutParagraphsBuilderForm extends FormBase {

  /**
   * A layout paragraphs layout object.
   *
   * @var \Drupal\layout_paragraphs\LayoutParagraphsLayout
   */
  protected $layoutParagraphsLayout;

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

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

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

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

  /**
   * Builds the layout paragraphs builder form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
   *   The layout paragraphs layout object.
   */
  public function buildForm(array $form, FormStateInterface $form_state, LayoutParagraphsLayout $layout_paragraphs_layout = NULL) {
    $this->layoutParagraphsLayout = $layout_paragraphs_layout;
    $form['layout_paragraphs_builder_ui'] = [
      '#type' => 'layout_paragraphs_builder',
      '#layout_paragraphs_layout' => $this->layoutParagraphsLayout,
    ];
    $form['#attributes']['data-lpb-form-id'] = Html::getId($this->layoutParagraphsLayout
      ->id());
    $form['actions'] = [
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Save'),
        '#ajax' => [
          'callback' => '::save',
        ],
        '#attributes' => [
          'class' => [
            'button--primary',
          ],
        ],
      ],
      'close' => [
        '#type' => 'button',
        '#value' => $this
          ->t('Close'),
        '#ajax' => [
          'callback' => '::close',
        ],
      ],
    ];
    return $form;
  }

  /**
   * Ajax callback.
   *
   * Closes the builder and returns the rendered layout.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An ajax command.
   */
  public function close(array $form, FormStateInterface $form_state) {
    $this->tempstore
      ->delete($this->layoutParagraphsLayout);
    $view_mode = $this->layoutParagraphsLayout
      ->getSetting('reference_field_view_mode', 'default');
    $rendered_layout = $this->layoutParagraphsLayout
      ->getParagraphsReferenceField()
      ->view($view_mode);
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand('[data-lpb-form-id="' . $form['#attributes']['data-lpb-form-id'] . '"]', $rendered_layout));
    return $response;
  }

  /**
   * Ajax callback.
   *
   * Displays a message when the entity is saved.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An ajax command.
   */
  public function save(array $form, FormStateInterface $form_state) {
    $entity = $this->layoutParagraphsLayout
      ->getEntity();
    $response = new AjaxResponse();
    $t_args = [
      '@type' => $entity
        ->getEntityType()
        ->getLabel(),
      '%title' => $entity
        ->toLink()
        ->toString(),
    ];
    $response
      ->addCommand(new MessageCommand($this
      ->t('@type %title has been updated.', $t_args)));
    $response
      ->addCommand(new ReplaceCommand('[data-lpb-form-id="' . $form['#attributes']['data-lpb-form-id'] . '"]', $form));
    return $response;
  }

  /**
   * {@inheritDoc}
   *
   * Saves the layout to its parent entity.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this->layoutParagraphsLayout
      ->getEntity();
    $field_name = $this->layoutParagraphsLayout
      ->getFieldName();
    $entity->{$field_name} = $this->layoutParagraphsLayout
      ->getParagraphsReferenceField();
    $entity
      ->save();
    $this->layoutParagraphsLayout
      ->setParagraphsReferenceField($entity->{$field_name});
    $this->tempstore
      ->set($this->layoutParagraphsLayout);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
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
LayoutParagraphsBuilderForm::$layoutParagraphsLayout protected property A layout paragraphs layout object.
LayoutParagraphsBuilderForm::$tempstore protected property The layout paragraphs layout tempstore service.
LayoutParagraphsBuilderForm::buildForm public function Builds the layout paragraphs builder form. Overrides FormInterface::buildForm
LayoutParagraphsBuilderForm::close public function Ajax callback.
LayoutParagraphsBuilderForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LayoutParagraphsBuilderForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LayoutParagraphsBuilderForm::save public function Ajax callback.
LayoutParagraphsBuilderForm::submitForm public function Saves the layout to its parent entity. Overrides FormInterface::submitForm
LayoutParagraphsBuilderForm::__construct public function
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.