You are here

public function PartyController::setLabel in Party 8.2

Set the label on a Party object using the first label plugin (when ordered by weight) that returns a non-empty value.

This method optionally saves the label to the database as well as generating it, this allows the function to be called independantly of party save. Also, as most Party label plugins require the party to be saved to function correctly, this avoids having to go through the Party::save() method multiple times.

Parameters

$party: A party object.

bool $store: Whether we should store the new label.

1 call to PartyController::setLabel()
PartyController::invoke in includes/party.entity.inc
Implements EntityAPIControllerInterface.

File

includes/party.entity.inc, line 64
Contains the controller classes for Party entities.

Class

PartyController
The API controller class for the Party entity.

Code

public function setLabel($party, $store = TRUE) {
  module_load_include('inc', 'party', 'party.admin');

  // Get the label plugins.
  $label_plugins = party_settings_get_party_label_plugins();

  // Try each plugin, lowest weight first, until a plugin is
  // found that returns a non-empty value.
  foreach ($label_plugins as $path => $label_plugin) {
    $label_callback = $label_plugin['label callback'];
    $label = $label_callback($party);
    if (!empty($label)) {
      $party->label = $label;
      break;
    }
  }

  // No label plugins returned a label, use the party id if possible.
  if (empty($label) && isset($party->id)) {
    $party->label = t('Party @pid', array(
      '@pid' => $party->id,
    ));
  }
  if ($store && isset($party->pid)) {

    // Save the label to the database.
    db_query('UPDATE {party} SET label = :label WHERE pid = :pid', array(
      ':label' => $party->label,
      ':pid' => $party->pid,
    ));
    $this
      ->invoke('update', $party);
  }
}