/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { // Save as a new revision if requested to do so. if (!$form_state->isValueEmpty('revision')) { $this->entity->setNewRevision(); } $insert = $this->entity->isNew(); $this->entity->save(); $context = ['@type' => $this->entity->bundle(), '%info' => $this->entity->label()]; $logger = $this->logger($this->entity->id()); $bundle_entity = $this->getBundleEntity(); $t_args = ['@type' => $bundle_entity ? $bundle_entity->label() : 'None', '%info' => $this->entity->label()]; if ($insert) { $logger->notice('@type: added %info.', $context); drupal_set_message($this->t('@type %info has been created.', $t_args)); } else { $logger->notice('@type: updated %info.', $context); drupal_set_message($this->t('@type %info has been updated.', $t_args)); } if ($this->entity->id()) { $form_state->setValue('id', $this->entity->id()); $form_state->set('id', $this->entity->id()); if ($this->entity->getEntityType()->hasLinkTemplate('collection')) { $form_state->setRedirectUrl($this->entity->toUrl('collection')); } else { $form_state->setRedirectUrl($this->entity->toUrl('canonical')); } } else { // In the unlikely case something went wrong on save, the entity will be // rebuilt and entity form redisplayed. drupal_set_message($this->t('The entity could not be saved.'), 'error'); $form_state->setRebuild(); } }
/** * Initialize the form state and the entity before the first form build. */ protected function init(FormStateInterface $form_state, EntityInterface $entity, $field_name) { // @todo Rather than special-casing $node->revision, invoke prepareEdit() // once https://www.drupal.org/node/1863258 lands. if ($entity->getEntityTypeId() == 'node') { $node_type = $this->nodeTypeStorage->load($entity->bundle()); $entity->setNewRevision($node_type->isNewRevision()); $entity->revision_log = NULL; } $form_state->set('entity', $entity); $form_state->set('field_name', $field_name); // Fetch the display used by the form. It is the display for the 'default' // form mode, with only the current field visible. $display = EntityFormDisplay::collectRenderDisplay($entity, 'default'); foreach ($display->getComponents() as $name => $options) { if ($name != $field_name) { $display->removeComponent($name); } } $form_state->set('form_display', $display); }
/** * {@inheritdoc} */ protected function doSave($id, EntityInterface $entity) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if ($entity->isNew()) { // Ensure the entity is still seen as new after assigning it an id, while // storing its data. $entity->enforceIsNew(); if ($this->entityType->isRevisionable()) { $entity->setNewRevision(); } $return = SAVED_NEW; } else { // @todo Consider returning a different value when saving a non-default // entity revision. See https://www.drupal.org/node/2509360. $return = $entity->isDefaultRevision() ? SAVED_UPDATED : FALSE; } $this->populateAffectedRevisionTranslations($entity); $this->doSaveFieldItems($entity); return $return; }
/** * Initialize the form state and the entity before the first form build. */ protected function init(array &$form_state, EntityInterface $entity, $field_name) { // @todo Rather than special-casing $node->revision, invoke prepareEdit() // once http://drupal.org/node/1863258 lands. if ($entity->getEntityTypeId() == 'node') { $node_type_settings = $this->nodeTypeStorage->load($entity->bundle())->getModuleSettings('node'); $options = isset($node_type_settings['options']) ? $node_type_settings['options'] : array(); $entity->setNewRevision(!empty($options['revision'])); $entity->revision_log = NULL; } $form_state['entity'] = $entity; $form_state['field_name'] = $field_name; // Fetch the display used by the form. It is the display for the 'default' // form mode, with only the current field visible. $display = EntityFormDisplay::collectRenderDisplay($entity, 'default'); foreach ($display->getComponents() as $name => $optipns) { if ($name != $field_name) { $display->removeComponent($name); } } $form_state['form_display'] = $display; }
/** * {@inheritdoc} * * @todo Revisit this logic with forward revisions in mind. */ protected function doSave($id, EntityInterface $entity) { if ($entity->_rev->is_stub) { $entity->isDefaultRevision(TRUE); } else { // Enforce new revision if any module messed with it in a hook. $entity->setNewRevision(); // Decide whether or not this is the default revision. if (!$entity->isNew()) { $default_rev = \Drupal::service('entity.index.rev.tree')->getDefaultRevision($entity->uuid()); if ($entity->_rev->value == $default_rev) { $entity->isDefaultRevision(TRUE); } else { $entity->isDefaultRevision(FALSE); } } } return parent::doSave($id, $entity); }