You are here

final public function PartyDefaultDataSet::attachEntity in Party 7

Same name and namespace in other branches
  1. 8.2 includes/party.data.inc \PartyDefaultDataSet::attachEntity()

Attach an entity to the party

This method puts the entity in the right place in the $entities array. NB: This does not save the new order, you must call PartyDefaultData::save() to do that. This method cannot be overloaded and any extensions of this class can make use of PartyDefaultDataSet::preAttach() and PartyDefaultDataSet::postAttach() to perform any additional logic required.

Parameters

object $entity: The entity we're attaching.

string $method: The $method we're using can be one of:

  • 'append' (default): the entity will be added to the end of the list.
  • 'prepend': the entity will be added at the front of the list.
  • 'insert': the entity will be inserted at $delta.

bool $reattach: If this entity is already attached, should we remove it and then reattach using the requested method. Defaults to FALSE.

int $delta: If the $method is set to insert. This is the target delta of the attached entity.

Return value

$this

See also

PartyDefaultDataSet::preAttach()

PartyDefaultDataSet::postAttach()

1 call to PartyDefaultDataSet::attachEntity()
PartyDefaultDataSet::getEntity in includes/party.data.inc
Get a particular attached entity

File

includes/party.data.inc, line 618
Provides the default class for managing party - Attached entity relationships.

Class

PartyDefaultDataSet
Class PartyDefaultDataSet

Code

public final function attachEntity($entity, $method = 'append', $reattach = FALSE, $delta = 0) {

  // Fire any pre attach logic
  $this
    ->preAttach($entity, $method, $delta);

  // Check if this entity is already attached
  $existing_delta = $this
    ->getEntityDelta($entity);
  if ($existing_delta !== FALSE) {
    if ($reattach) {

      // Do this manually as we don't want to trigger the pre/post callbacks.
      unset($this->entities[$existing_delta]);
      $this->entities = array_values($this->entities);
      $existing_delta = FALSE;
    }
    else {

      // Let's make sure our attached entity if fully loaded and update it if necessary
      $this->entities[$existing_delta] = $entity;
    }
  }

  // Only attach if it wasn't already and hasn't been detached.
  if ($existing_delta === FALSE) {

    // Add important information to the entity.
    $entity->data_set_name = $this->data_set;
    $entity->party_attaching_party = $this->party->pid;
    switch ($method) {
      case 'append':
        $this->entities[] = $entity;
        break;
      case 'prepend':
        array_unshift($this->entities, $entity);
        break;
      case 'insert':

        // Put the entity and its availability in the right place
        array_splice($this->entities, $delta, 0, $entity);
        break;
    }
    $this
      ->invoke('attach', $entity, $delta, $method, $reattach);
  }

  // Fire any post attach logic
  $this
    ->postAttach($entity, $method, $delta);
  return $this;
}