public function EckEntityBundleForm::save in Entity Construction Kit (ECK) 8
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ EntityBundle/ EckEntityBundleForm.php, line 202
Class
- EckEntityBundleForm
- Form controller for ECK entity bundle forms.
Namespace
Drupal\eck\Form\EntityBundleCode
public function save(array $form, FormStateInterface $form_state) {
$type = $this->entity;
$type->type = trim($type
->id());
$type->name = trim($type->name);
$status = $type
->save();
$t_args = [
'%name' => $type
->label(),
];
if ($status == SAVED_UPDATED) {
\Drupal::messenger()
->addMessage($this
->t('The entity bundle %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
\Drupal::messenger()
->addMessage($this
->t('The entity bundle %name has been added.', $t_args));
$context = array_merge($t_args, [
'link' => Link::fromTextAndUrl($this
->t('View'), new Url('eck.entity.' . $type
->getEntityType()
->getBundleOf() . '_type.list'))
->toString(),
]);
$this
->logger($this->entity
->getEntityTypeId())
->notice('Added entity bundle %name.', $context);
}
// Update field labels definition.
$bundle_fields = $this->entityFieldManager
->getFieldDefinitions($type
->getEntityType()
->getBundleOf(), $type
->id());
$base_fields = $this->entityFieldManager
->getBaseFieldDefinitions($type
->getEntityType()
->getBundleOf());
foreach ([
'created',
'changed',
'uid',
'title',
'status',
] as $field) {
if (!$form_state
->hasValue($field . '_title_override')) {
continue;
}
$has_changed = FALSE;
$field_definition = $bundle_fields[$field];
$field_config = $field_definition
->getConfig($type
->id());
$label = $form_state
->getValue($field . '_title_override') ?: $base_fields[$field]
->getLabel();
if ($field_definition
->getLabel() != $label) {
$field_config
->setLabel($label);
$has_changed = TRUE;
}
$description = $form_state
->getValue($field . '_description_override') ?: $base_fields[$field]
->getDescription();
if ($field_definition
->getDescription() != $description) {
$field_config
->setDescription($description);
$has_changed = TRUE;
}
if ($has_changed) {
$field_config
->save();
}
}
$this->entityFieldManager
->clearCachedFieldDefinitions();
$form_state
->setRedirect('eck.entity.' . $type
->getEntityType()
->getBundleOf() . '_type.list');
}