View source
<?php
namespace Drupal\dynamic_layouts\Form;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\dynamic_layouts\DynamicLayoutInterface;
use Drupal\dynamic_layouts\DynamicLayoutConstants as Constants;
class DynamicLayoutForm extends EntityForm {
protected $renderer;
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'));
}
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}
public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form['#attached']['library'][] = 'dynamic_layouts/dynamic_layouts';
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#cache'] = [
'max-age' => 0,
];
$form['#tree'] = TRUE;
$entity_is_new = FALSE;
if ($entity
->isNew()) {
$entity_is_new = TRUE;
}
$this
->addSettingsFormFields($form, $entity);
$form[Constants::DYNAMIC_LAYOUT] = array(
'#type' => 'vertical_tabs',
'#title' => t('Dynamic layout'),
);
if (!$entity_is_new) {
$url = $entity
->toUrl()
->toString();
$form['#action'] = $url;
$form[Constants::LAYOUT] = [
'#type' => 'container',
'#title' => $this
->t('Layout'),
'#tree' => TRUE,
'#prefix' => '<div id="js-ajax-elements-wrapper">',
'#suffix' => '</div>',
];
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP] = [
'#type' => 'details',
'#title' => $this
->t('Layout'),
'#tree' => TRUE,
'#group' => Constants::DYNAMIC_LAYOUT,
'#description' => t('To change the default names of the columns, click "Edit column".'),
'#attributes' => [
'class' => [
'layout-group',
],
],
];
$addRowTitle = t('Add row');
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['add_row_top'] = [
'#type' => 'link',
'#name' => 'add_row',
'#title' => $addRowTitle,
'#prefix' => '<div class="add-row-wrapper">',
'#suffix' => '</div>',
'#url' => Url::fromRoute('dynamic_layouts.add_row', [
'dynamic_layout_id' => $entity
->id(),
]),
'#attributes' => [
'class' => [
'btn',
'add-row',
'use-ajax',
],
'title' => $addRowTitle,
],
];
}
$this
->addRowFormFields($form);
$form_state
->setCached(FALSE);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Testing'),
];
return parent::form($form, $form_state);
}
protected function addSettingsFormFields(array &$form, DynamicLayoutInterface $entity) {
$layout_tab_weight = 1;
$entity_is_new = FALSE;
if ($entity
->isNew()) {
$entity_is_new = TRUE;
$layout_tab_weight = 0;
}
$form[Constants::GENERAL_SETTING] = array(
'#type' => 'details',
'#title' => t('General settings'),
'#group' => Constants::DYNAMIC_LAYOUT,
'#weight' => $layout_tab_weight,
);
$form[Constants::GENERAL_SETTING]['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Layout name'),
'#default_value' => $entity
->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this
->t('The name for this layout.'),
];
$form[Constants::GENERAL_SETTING]['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity
->id(),
'#required' => TRUE,
'#disabled' => !$entity
->isNew(),
'#size' => 30,
'#maxlength' => 64,
'#machine_name' => [
'exists' => [
'\\Drupal\\dynamic_layouts\\Entity\\DynamicLayout',
'load',
],
'source' => [
Constants::GENERAL_SETTING,
'label',
],
],
];
$form[Constants::GENERAL_SETTING][Constants::CATEGORY] = [
'#type' => 'textfield',
'#title' => $this
->t('Layout category'),
'#default_value' => $entity
->getCategory(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => $this
->t('The category of this layout.'),
];
$form[Constants::GENERAL_SETTING][Constants::DEFAULT_ROW_CLASS] = [
'#type' => 'textfield',
'#title' => $this
->t('Default row class'),
'#default_value' => $entity
->getDefaultRowClass(),
'#size' => 30,
'#maxlength' => 64,
'#required' => TRUE,
'#description' => $this
->t('This class will be used on every row.'),
];
$form[Constants::GENERAL_SETTING][Constants::DEFAULT_COLUMN_CLASS] = [
'#type' => 'textfield',
'#title' => $this
->t('Default column class'),
'#default_value' => $entity
->getDefaultColumnClass(),
'#size' => 30,
'#maxlength' => 64,
'#description' => $this
->t('This class will be used on every column.'),
];
if ($entity_is_new) {
$form[Constants::GENERAL_SETTING][Constants::START_ROWS_COUNT] = [
'#type' => 'number',
'#title' => $this
->t('Layout rows'),
'#description' => $this
->t('After saving you can add, remove configure the rows'),
'#min' => 1,
'#max' => 50,
'#default_value' => 1,
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
];
}
}
protected function addRowFormFields(array &$form) {
$entity = $this->entity;
if ($rows = $entity
->getRows()) {
$wrapper_classes = $this
->getWrapperClasses();
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'] = [
'#type' => 'markup',
'#prefix' => '<div class="' . $wrapper_classes . '">',
'#suffix' => '</div>',
];
$row_count = 1;
foreach ($rows as $row) {
if (!isset($row['row_id'])) {
continue;
}
$row_id = $row['row_id'];
$row['row_count'] = $row_count;
$addLinkOptions = $editLinkOptions = $deleteLinkOptions = [
'attributes' => [
'class' => [
'use-ajax',
'btn',
],
],
];
$addLinkTitle = t('Add column');
$editLinkTitle = t('Edit row');
$deleteLinkTitle = t('Delete row');
$addLinkOptions['attributes']['class'][] = 'add-column-link';
$editLinkOptions['attributes']['class'][] = 'edit-link';
$deleteLinkOptions['attributes']['class'][] = 'delete-link';
$addLinkOptions['attributes']['title'] = $addLinkTitle;
$editLinkOptions['attributes']['title'] = $editLinkTitle;
$deleteLinkOptions['attributes']['title'] = $deleteLinkTitle;
$row['add_column_link'] = $entity
->getRowLink($row_id, 'dynamic_layouts.add_column', $addLinkTitle, $addLinkOptions);
$row['edit_row_link'] = $entity
->getRowLink($row_id, 'dynamic_layouts.edit_row_modal_form', $editLinkTitle, $editLinkOptions);
$row['delete_row_link'] = $entity
->getRowLink($row_id, 'dynamic_layouts.delete_row', $deleteLinkTitle, $deleteLinkOptions);
$elements = [
'#theme' => 'dynamic_layouts_backend',
'#row' => $row,
];
$rendered_row = $this->renderer
->render($elements);
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'][$row_id] = [
'#type' => 'inline_template',
'#template' => '{{ row | raw }}',
'#context' => [
'row' => $rendered_row,
],
];
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'][$row_id]['actions'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'actions-wrapper',
'col-12',
],
],
];
$row_count++;
}
$addRowTitle = t('Add row');
$form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['add_row_bottom'] = [
'#type' => 'link',
'#name' => 'add_row',
'#title' => $addRowTitle,
'#prefix' => '<div class="add-row-wrapper">',
'#suffix' => '</div>',
'#url' => Url::fromRoute('dynamic_layouts.add_row', [
'dynamic_layout_id' => $entity
->id(),
]),
'#attributes' => [
'class' => [
'btn',
'add-row',
'use-ajax',
],
'title' => $addRowTitle,
],
];
}
}
protected function actions(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$submit_title = $this
->t('Save layout');
if ($entity
->isNew()) {
$submit_title = $this
->t('Save and configure rows');
}
$actions['submit'] = [
'#type' => 'submit',
'#value' => $submit_title,
'#submit' => [
'::submitForm',
'::save',
],
];
if (!$this->entity
->isNew() && $this->entity
->hasLinkTemplate('delete-form')) {
$route_info = $this->entity
->toUrl('delete-form');
if ($this
->getRequest()->query
->has('destination')) {
$query = $route_info
->getOption('query');
$query['destination'] = $this
->getRequest()->query
->get('destination');
$route_info
->setOption('query', $query);
}
$actions['delete'] = [
'#type' => 'link',
'#title' => $this
->t('Delete'),
'#access' => $this->entity
->access('delete'),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
];
$actions['delete']['#url'] = $route_info;
}
return $actions;
}
public function getWrapperClasses() {
$wrapper_classes_arr = [
'container-fluid',
];
$frontend_library_class = 'custom';
if ($settings = $this->entityTypeManager
->getStorage('dynamic_layout_settings')
->load('settings')) {
$frontend_library = $settings
->getFrontendLibrary();
if ($frontend_library == 'bootstrap') {
$wrapper_classes_arr[] = $frontend_library_class . '-12';
}
else {
if ($column_count = $settings
->getGridColumnCount()) {
$wrapper_classes_arr[] = $frontend_library . '-' . $column_count;
}
}
}
$wrapper_classes_arr[] = $frontend_library_class;
return implode(' ', $wrapper_classes_arr);
}
public function setFormValues($general_settings, DynamicLayoutInterface $entity, $entity_is_new) {
if (isset($general_settings['label']) && $general_settings['label']) {
$entity
->set('label', trim($general_settings['label']));
}
if (isset($general_settings['id']) && $general_settings['id']) {
$entity
->set('id', trim($general_settings['id']));
}
if (isset($general_settings[Constants::CATEGORY]) && $general_settings[Constants::CATEGORY]) {
$entity
->set(Constants::CATEGORY, trim($general_settings[Constants::CATEGORY]));
}
if (!$entity_is_new) {
if (isset($general_settings[Constants::DEFAULT_COLUMN_CLASS]) && $general_settings[Constants::DEFAULT_COLUMN_CLASS]) {
$entity
->setDefaultColumnClass(trim($general_settings[Constants::DEFAULT_COLUMN_CLASS]));
}
if (isset($general_settings[Constants::DEFAULT_ROW_CLASS]) && $general_settings[Constants::DEFAULT_ROW_CLASS]) {
$entity
->setDefaultRowClass(trim($general_settings[Constants::DEFAULT_ROW_CLASS]));
}
}
$entity
->save();
}
public function save(array $form, FormStateInterface $form_state) {
if (!($general_settings = $form_state
->getValue(Constants::GENERAL_SETTING))) {
return NULL;
}
$entity = $this->entity;
$entity_is_new = FALSE;
if ($entity
->isNew()) {
$entity_is_new = TRUE;
}
$this
->setFormValues($general_settings, $entity, $entity_is_new);
$status = $entity
->save();
if ($entity_is_new && isset($general_settings[Constants::START_ROWS_COUNT]) && $general_settings[Constants::START_ROWS_COUNT]) {
$entity
->addStartingRows($general_settings);
}
$action = $status == SAVED_UPDATED ? 'updated' : 'added';
$this
->messenger()
->addStatus($this
->t('Layout %label has been %action.', [
'%label' => $entity
->label(),
'%action' => $action,
]));
$this
->logger(Constants::DYNAMIC_LAYOUT)
->notice('Layout %label has been %action.', array(
'%label' => $entity
->label(),
'%action' => $action,
));
\Drupal::service('plugin.cache_clearer')
->clearCachedDefinitions();
if (!$entity_is_new) {
$form_state
->setRedirect('dynamic_layout.dynamic_layout_list');
}
else {
$form_state
->setRedirect('entity.dynamic_layout.edit_form', [
Constants::DYNAMIC_LAYOUT => $entity
->id(),
]);
}
}
}