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
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\layout_paragraphs\Form\LayoutParagraphsBuilderForm
Expanded class hierarchy of LayoutParagraphsBuilderForm
File
- src/
Form/ LayoutParagraphsBuilderForm.php, line 21
Namespace
Drupal\layout_paragraphs\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LayoutParagraphsBuilderForm:: |
protected | property | A layout paragraphs layout object. | |
LayoutParagraphsBuilderForm:: |
protected | property | The layout paragraphs layout tempstore service. | |
LayoutParagraphsBuilderForm:: |
public | function |
Builds the layout paragraphs builder form. Overrides FormInterface:: |
|
LayoutParagraphsBuilderForm:: |
public | function | Ajax callback. | |
LayoutParagraphsBuilderForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
LayoutParagraphsBuilderForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
LayoutParagraphsBuilderForm:: |
public | function | Ajax callback. | |
LayoutParagraphsBuilderForm:: |
public | function |
Saves the layout to its parent entity. Overrides FormInterface:: |
|
LayoutParagraphsBuilderForm:: |
public | function | ||
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |