function NodeTypeForm::save

Same name and namespace in other branches
  1. 9 core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::save()
  2. 8.9.x core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::save()
  3. 10 core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::save()

Overrides EntityForm::save

File

core/modules/node/src/NodeTypeForm.php, line 224

Class

NodeTypeForm
Form handler for node type forms.

Namespace

Drupal\node

Code

public function save(array $form, FormStateInterface $form_state) {
    $type = $this->entity;
    $type->setNewRevision($form_state->getValue([
        'options',
        'revision',
    ]));
    $type->set('type', trim($type->id()));
    $type->set('name', trim($type->label()));
    $status = $type->save();
    $t_args = [
        '%name' => $type->label(),
    ];
    if ($status == SAVED_UPDATED) {
        $this->messenger()
            ->addStatus($this->t('The content type %name has been updated.', $t_args));
    }
    elseif ($status == SAVED_NEW) {
        node_add_body_field($type);
        $this->messenger()
            ->addStatus($this->t('The content type %name has been added.', $t_args));
        $context = array_merge($t_args, [
            'link' => $type->toLink($this->t('View'), 'collection')
                ->toString(),
        ]);
        $this->logger('node')
            ->notice('Added content type %name.', $context);
    }
    $fields = $this->entityFieldManager
        ->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->entityTypeManager
        ->getStorage('node')
        ->create([
        'type' => $type->id(),
    ]);
    foreach ([
        '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->entityFieldManager
        ->clearCachedFieldDefinitions();
    $form_state->setRedirectUrl($type->toUrl('collection'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.