You are here

public function PartyStorageController::invokeHook in Party 8.2

Implements EntityAPIControllerInterface.

Add in our additional logic for party labels. This is in here rather than PartyStorageController::save() so that we can get the label set as early as possible. The tricky case is when a plugin requires the party to be saved or we fall back to pid which also requires the party to be saved. For those cases, we need to update the label immediately after we write the record but before we invoke modules' implementation of hook_entity_insert(). However, in the more common use case, we ideally want the label to be set before hook_entity_presave() is invoked.

1 call to PartyStorageController::invokeHook()
PartyStorageController::setLabel in lib/Drupal/party/PartyStorageController.php
Set the label on a Party object using the first label plugin (when ordered by weight) that returns a non-empty value.

File

lib/Drupal/party/PartyStorageController.php, line 34
Definition of Drupal\party\PartyStorageController.

Class

PartyStorageController
Controller class for users.

Namespace

Drupal\party

Code

public function invokeHook($hook, EntityInterface $party) {

  // In presave, trigger our label and primary fields update without a save.
  if ($hook == 'presave') {
    $this
      ->setLabel($party, FALSE);
    $this
      ->setPrimaryFields($party, FALSE);
  }

  // In insert, if our label is empty, set our fallback and save.
  // @TODO: due to the use of the PID party label plugin as a stopper, we
  // we need to compare it to a stopper response from the plugin. Once we
  // rework the label plugins we can get rid of this uglyness and check
  // for an empty.
  if ($hook == 'insert' && $party->label == t('Party')) {
    $party->label = t('Party @pid', array(
      '@pid' => $party->pid,
    ));
    db_query('UPDATE {party} SET label = :label WHERE pid = :pid', array(
      ':label' => $party->label,
      ':pid' => $party
        ->id(),
    ));
  }

  // Trigger the rest of the invoke action.
  parent::invokeHook($hook, $party);
}