You are here

public function PartyStorageController::setPrimaryFields in Party 8.2

Set the primary fields for the party.

Parameters

$party: A party object.

1 call to PartyStorageController::setPrimaryFields()
PartyStorageController::invokeHook in lib/Drupal/party/PartyStorageController.php
Implements EntityAPIControllerInterface.

File

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

Class

PartyStorageController
Controller class for users.

Namespace

Drupal\party

Code

public function setPrimaryFields($party, $store = TRUE) {
  $primary_fields = variable_get('party_primary_fields', array());

  // Update the email.
  if (!empty($primary_fields['email'])) {

    // Extract our info from the setting.
    list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email']);
    $controller = drupal_container()
      ->get('plugin.manager.partydata')
      ->createInstance($party);
    if ($entity = $controller
      ->getEntity()) {
      if ($field_name) {
        $items = field_get_items($controller
          ->getDataInfo('entity type'), $entity, $field_name);
        if ($items) {
          $item = reset($items);
          $party->mail = $item[$column];
        }
        else {
          $party->mail = NULL;
        }
      }
      else {
        $party->mail = $entity->{$column};
      }
    }
  }
  if (empty($party->email) && !empty($primary_fields['email2'])) {

    // Extract our info from the setting.
    list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email2']);
    $controller = drupal_container()
      ->get('plugin.manager.partydata')
      ->createInstance($party);
    if ($entity = $controller
      ->getEntity()) {
      if ($field_name) {
        $items = field_get_items($controller
          ->getDataInfo('entity type'), $entity, $field_name);
        if ($items) {
          $item = reset($items);
          $party->mail = $item[$column];
        }
        else {
          $party->mail = NULL;
        }
      }
      else {
        $party->mail = $entity->{$column};
      }
    }
  }
  if ($store && isset($party->pid)) {

    // Save the primary feilds to the database.
    db_query('UPDATE {party} SET mail = :mail WHERE pid = :pid', array(
      ':mail' => $party->mail,
      ':pid' => $party
        ->id(),
    ));
  }
}