You are here

final public function PartyDefaultDataSet::attachEntity in Party 8.2

Same name and namespace in other branches
  1. 7 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 458
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
  $delta = $this
    ->getEntityDelta($entity);
  if ($delta !== FALSE) {
    if ($reattach) {

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

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

  // Only attach if it wasn't already and hasn't been detached.
  if ($delta === FALSE) {
    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;
    }
  }

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