public function FlaggingCollection::preSave in Flag Lists 8
Same name and namespace in other branches
- 4.0.x src/Entity/FlaggingCollection.php \Drupal\flag_lists\Entity\FlaggingCollection::preSave()
Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
Throws
\Exception When there is a problem that should prevent saving the entity.
Overrides ContentEntityBase::preSave
See also
\Drupal\Core\Field\FieldItemListInterface::preSave()
File
- src/
Entity/ FlaggingCollection.php, line 101
Class
- FlaggingCollection
- Defines the Flagging collection entity.
Namespace
Drupal\flag_lists\EntityCode
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation
->getOwner()) {
$translation
->setOwnerId(0);
}
}
// If no revision author has been set explicitly,
// make the flagging_collection owner the
// revision author.
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
// Copy all field data from the base_flag, Template Flag
// and store it in the a new Flag, referenced as related_flag.
$flagListService = \Drupal::service('flaglists');
$flagService = \Drupal::service('flag');
$baseFlag = $flagListService
->getFlagForListById($this
->getBaseFlag()
->id());
$flag_to_use = $flagService
->getFlagById($baseFlag
->get('base_flag'));
// Copy its important content to a array.
$template['label'] = $this
->label();
if ($this
->isNew()) {
// The id = machine id must be conformant with the rules.
$template['id'] = strtolower($this
->label());
$template['id'] = preg_replace("/[^a-z0-9_]/", "_", $template['id']);
$this
->setRelatedFlag($template['id']);
}
else {
// The Flagging Collection exist so reuse the old id.
$template['id'] = $this
->getRelatedFlag()
->id();
}
$template['global'] = $flag_to_use
->isGlobal();
$template['flag_short'] = $flag_to_use
->getShortText('flag');
$template['flag_long'] = $flag_to_use
->getLongText('flag');
$template['flag_message'] = $flag_to_use
->getMessage('flag');
$template['unflag_short'] = $flag_to_use
->getShortText('unflag');
$template['unflag_long'] = $flag_to_use
->getLongText('unflag');
$template['unflag_message'] = $flag_to_use
->getMessage('unflag');
$template['access'] = [];
$template['access']['bundles'] = '';
$template['unflag_denied_text'] = '';
$template['link_type'] = $flag_to_use
->getLinkTypePlugin()
->getPluginId();
$template['flag_type'] = $flag_to_use
->getFlagTypePlugin()
->getPluginId();
$template['entity_type'] = $flag_to_use
->getFlaggableEntityTypeId();
$template['flagTypeConfig'] = $flag_to_use
->getFlagTypePlugin()
->getConfiguration();
// Use the array to create a new, possibly uniq, flag.
$flag = new flag($template, 'flag');
$flag
->save();
}