public function MaestroTemplateFormBase::save in Maestro 3.x
Same name and namespace in other branches
- 8.2 src/Form/MaestroTemplateFormBase.php \Drupal\maestro\Form\MaestroTemplateFormBase::save()
Overrides Drupal\Core\Entity\EntityFormController::save().
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The form's form state.
Overrides EntityForm::save
1 call to MaestroTemplateFormBase::save()
- MaestroTemplateAddForm::save in src/
Form/ MaestroTemplateAddForm.php - Overrides MaestroTemplateFormBase::save.
1 method overrides MaestroTemplateFormBase::save()
- MaestroTemplateAddForm::save in src/
Form/ MaestroTemplateAddForm.php - Overrides MaestroTemplateFormBase::save.
File
- src/
Form/ MaestroTemplateFormBase.php, line 415
Class
- MaestroTemplateFormBase
- Class MaestroTemplateFormBase.
Namespace
Drupal\maestro\FormCode
public function save(array $form, FormStateInterface $form_state) {
$isModal = $this
->getRequest()
->get('is_modal');
// Remove the 'new' variable values here so that the save, well, saves them.
// form_state->values['variables'] holds the saved variables
// we need to ensure that the variables at this stage, since the parent validation has happened
// is properly formatted to remove the 'new' key and add that to the numerical keys.
$variables = $form_state
->getValue('variables');
$new = $variables['new_fieldset']['new'];
unset($variables['new_fieldset']);
if (isset($new['variable_id']) && !empty($new['variable_id'])) {
// We have a new variable here.
$variables[$new['variable_id']] = $new;
}
// Now to handle deletion.
foreach ($variables as $key => $variable) {
if (array_key_exists('delete', $variable) && isset($variable['delete'])) {
if ($variable['delete'] == 1) {
unset($variables[$key]);
}
else {
unset($variables[$key]['delete']);
}
}
}
$this->entity->variables = $variables;
// Now handle attached views and handle the ordering properly.
$views_ordering_from_form = $form_state
->getValue('views_attached_to_template');
$existing_views = [];
if (isset($this->entity->views_attached)) {
foreach ($this->entity->views_attached as $machine_name => $arr) {
$existing_views[$arr['view_weight']] = $machine_name;
}
}
$views_ordering = [];
if (!empty($views_ordering_from_form)) {
foreach ($views_ordering_from_form as $key => $arr) {
if (is_numeric($key)) {
$views_ordering[$arr['weight']] = $existing_views[$key];
if ($arr['delete']) {
unset($views_ordering[$arr['weight']]);
}
}
}
}
ksort($views_ordering);
$views_attached = [];
$lowest_weight = 0;
foreach ($views_ordering as $weight => $machine_name) {
if ($weight <= $lowest_weight) {
$lowest_weight = $weight - 1;
}
$views_attached[$machine_name] = [
'view_machine_name' => $machine_name,
'view_weight' => $weight,
'view_display' => $this->entity->views_attached[$machine_name]['view_display'],
];
}
// Now if there's a new view, attach it.
if ($form_state
->getValue('add_view')) {
$views_attached[$form_state
->getValue('add_view')] = [
'view_machine_name' => $form_state
->getValue('add_view'),
'view_weight' => $lowest_weight,
'view_display' => $form_state
->getValue('add_view_display'),
];
}
$this->entity->views_attached = $views_attached;
$status = $this->entity
->save();
if ($status == SAVED_UPDATED) {
// If we edited an existing entity...
\Drupal::messenger()
->addMessage(t('Template %label has been updated.', [
'%label' => $this->entity
->label(),
]));
\Drupal::logger('maestro')
->notice('Template %label has been updated.', [
'%label' => $this->entity
->label(),
]);
if ($isModal == 'modal') {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
}
else {
// If we created a new entity...
\Drupal::messenger()
->addMessage(t('Template %label has been added.', [
'%label' => $this->entity
->label(),
]));
\Drupal::logger('maestro')
->notice('Template %label has been added.', [
'%label' => $this->entity
->label(),
]);
$form_state
->setRedirect('entity.maestro_template.list');
}
}