/** * @covers ::preSave */ public function testPreSave() { // This method is internal, so check for errors on calling it only. $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); // Our mocked entity->preSave() returns NULL, so assert that. $this->assertNull($this->entity->preSave($storage)); }
/** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); if ($this instanceof EntityWithPluginCollectionInterface) { // Any changes to the plugin configuration must be saved to the entity's // copy as well. foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) { $this->set($plugin_config_key, $plugin_collection->getConfiguration()); } } // Ensure this entity's UUID does not exist with a different ID, regardless // of whether it's new or updated. $matching_entities = $storage->getQuery()->condition('uuid', $this->uuid())->execute(); $matched_entity = reset($matching_entities); if (!empty($matched_entity) && $matched_entity != $this->id() && $matched_entity != $this->getOriginalId()) { throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this UUID is already used for '{$matched_entity}'"); } // If this entity is not new, load the original entity for comparison. if (!$this->isNew()) { $original = $storage->loadUnchanged($this->getOriginalId()); // Ensure that the UUID cannot be changed for an existing entity. if ($original && $original->uuid() != $this->uuid()) { throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this entity already exists with UUID '{$original->uuid()}'"); } } if (!$this->isSyncing()) { // Ensure the correct dependencies are present. If the configuration is // being written during a configuration synchronization then there is no // need to recalculate the dependencies. $this->calculateDependencies(); } }
/** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { // An entity requiring validation should not be saved if it has not been // actually validated. if ($this->validationRequired && !$this->validated) { // @todo Make this an assertion in https://www.drupal.org/node/2408013. throw new \LogicException('Entity validation was skipped.'); } else { $this->validated = FALSE; } parent::preSave($storage); }
/** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); // This is the easiest way to handle the unique internal path '<front>', // since a path marked as external does not need to match a route. $this->external = UrlHelper::isExternal($this->link_path) || $this->link_path == '<front>' ? 1 : 0; // Try to find a parent link. If found, assign it and derive its menu. $parent = $this->findParent($storage); if ($parent) { $this->plid = $parent->id(); $this->menu_name = $parent->menu_name; } else { $this->plid = 0; } // Directly fill parents for top-level links. if ($this->plid == 0) { $this->p1 = $this->id(); for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) { $parent_property = "p{$i}"; $this->{$parent_property} = 0; } $this->depth = 1; } else { if ($this->has_children && $this->original) { $limit = MENU_MAX_DEPTH - $storage->findChildrenRelativeDepth($this->original) - 1; } else { $limit = MENU_MAX_DEPTH - 1; } if ($parent->depth > $limit) { return FALSE; } $this->depth = $parent->depth + 1; $this->setParents($parent); } // Need to check both plid and menu_name, since plid can be 0 in any menu. if (isset($this->original) && ($this->plid != $this->original->plid || $this->menu_name != $this->original->menu_name)) { $storage->moveChildren($this); } // Find the route_name. if (!$this->external && !isset($this->route_name)) { $url = Url::createFromPath($this->link_path); $this->route_name = $url->getRouteName(); $this->route_parameters = $url->getRouteParameters(); } elseif (empty($this->link_path)) { $this->link_path = \Drupal::urlGenerator()->getPathFromRoute($this->route_name, $this->route_parameters); } }