public function NodeTypeForm::save in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::save()
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
- core/
modules/ node/ src/ NodeTypeForm.php, line 220 - Contains \Drupal\node\NodeTypeForm.
Class
- NodeTypeForm
- Form controller for node type forms.
Namespace
Drupal\nodeCode
public function save(array $form, FormStateInterface $form_state) {
$type = $this->entity;
$type
->setNewRevision($form_state
->getValue(array(
'options',
'revision',
)));
$type
->set('type', trim($type
->id()));
$type
->set('name', trim($type
->label()));
$status = $type
->save();
$t_args = array(
'%name' => $type
->label(),
);
if ($status == SAVED_UPDATED) {
drupal_set_message(t('The content type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
node_add_body_field($type);
drupal_set_message(t('The content type %name has been added.', $t_args));
$context = array_merge($t_args, array(
'link' => $type
->link($this
->t('View'), 'collection'),
));
$this
->logger('node')
->notice('Added content type %name.', $context);
}
$fields = $this->entityManager
->getFieldDefinitions('node', $type
->id());
// Update title field definition.
$title_field = $fields['title'];
$title_label = $form_state
->getValue('title_label');
if ($title_field
->getLabel() != $title_label) {
$title_field
->getConfig($type
->id())
->setLabel($title_label)
->save();
}
// Update workflow options.
// @todo Make it possible to get default values without an entity.
// https://www.drupal.org/node/2318187
$node = $this->entityManager
->getStorage('node')
->create(array(
'type' => $type
->id(),
));
foreach (array(
'status',
'promote',
'sticky',
) as $field_name) {
$value = (bool) $form_state
->getValue([
'options',
$field_name,
]);
if ($node->{$field_name}->value != $value) {
$fields[$field_name]
->getConfig($type
->id())
->setDefaultValue($value)
->save();
}
}
$this->entityManager
->clearCachedFieldDefinitions();
$form_state
->setRedirectUrl($type
->urlInfo('collection'));
}