You are here

public function FlaggingCollection::postSave in Flag Lists 4.0.x

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update 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.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides ContentEntityBase::postSave

File

src/Entity/FlaggingCollection.php, line 124

Class

FlaggingCollection
Defines the Flagging collection entity.

Namespace

Drupal\flag_lists\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // 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 (empty($this
    ->getRelatedFlag())) {
    $template['id'] = 'relflag' . '_' . $this
      ->getEntityKey('id') . '_' . $this
      ->getOwnerId();

    // We need to set the Related Flag on this entity.
    // Do it by loading a copy of it.
    // Defer the saving until after the flag is saved.
    $myCollection = $storage
      ->load($this
      ->getEntityKey('id'));
    $myCollection
      ->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['bundles'] = $flag_to_use
    ->getBundles();
  $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();
  if (!empty($myCollection)) {

    // If we have defered saving of our own copy, save it.
    $storage
      ->save($myCollection);
  }
}