You are here

class Bundle in Entity Construction Kit (ECK) 7.2

Same name and namespace in other branches
  1. 7.3 eck.classes.inc \Bundle

A bundle database object.

Hierarchy

Expanded class hierarchy of Bundle

1 string reference to 'Bundle'
eck__bundle__list in ./eck.bundle.inc
Page call back for the bundle overview table.

File

./eck.classes.inc, line 523
Classes for all the different objects used in ECK.

View source
class Bundle extends DBObject {

  /**
   * Constructor.
   */
  public function __construct() {
    parent::__construct('eck_bundle');
    $this->config = array();
  }

  /**
   * @param array $machineNames
   * @return Bundle[]
   */
  public static function loadMultipleByMachineName(array $machineNames) {
    $bundles = array();
    foreach ($machineNames as $machineName) {
      $bundle = self::loadByMachineName($machineName);
      if (!empty($bundle)) {
        $bundles[$machineName] = $bundle;
      }
    }
    return $bundles;
  }

  /**
   * Create a machine name.
   */
  private function createMachineName() {
    $this->machine_name = "{$this->entity_type}_{$this->name}";
  }

  /**
   * Create a label.
   */
  private function createLabel() {
    $name = $this->name;
    $pieces = explode("_", $name);
    $final = array();
    foreach ($pieces as $piece) {
      $final[] = ucfirst($piece);
    }
    $this->label = implode(" ", $final);
  }

  /**
   * Save the entity type.
   */
  public function save() {

    // Lets do some checks before the bundle is saved.
    if (isset($this->entity_type) && isset($this->name)) {
      $save = TRUE;

      // Lets set the machine name.
      $this
        ->createMachineName();

      // If this bundle isNew we need to check that it does not exist.
      // @todo we just need to change the field in the db to be unique.
      if ($this->isNew) {
        $bundle = Bundle::loadByMachineName($this->machine_name);
        if (!empty($bundle) && !$bundle->isNew) {
          $save = FALSE;
        }
      }
      if (!isset($this->label)) {
        $this
          ->createLabel();
      }
      if ($save) {
        parent::save();
        global $_eck_bundles_cache;
        $cache_enabled = isset($_eck_bundles_cache) ? TRUE : FALSE;
        if ($cache_enabled) {
          $_eck_bundles_cache
            ->reset();
        }
        eck_clean_up_request();
      }
      else {

        // @todo throw some error.
      }
    }
    else {

      // If the name an entity type are not set, we can not save the bundle.
      // @todo throw soem error or exception.
    }
  }

  /**
   * Delete the entity type.
   */
  public function delete() {

    // First delete all of the entities of this bundle.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $this->entity_type, '=')
      ->entityCondition('bundle', $this->name, '=');
    $results = $query
      ->execute();
    if (!empty($results)) {
      $ids = array_keys($results[$this->entity_type]);
      entity_delete_multiple($this->entity_type, $ids);
    }

    // Then we delete the bundle (field_instances).
    field_attach_delete_bundle($this->entity_type, $this->name);
    parent::delete();
    global $_eck_bundles_cache;
    $cache_enabled = isset($_eck_bundles_cache) ? TRUE : FALSE;
    if ($cache_enabled) {
      $_eck_bundles_cache
        ->reset();
    }
    eck_clean_up_request();
  }

  /**
   * This method returns a bundle object.
   *
   * @param string $machine_name
   *   A string composed of the entity type name and the bundle name,
   *   e.g., "{$entity_type_name}_{$bundle_name}".
   *
   * @return Bundle|Null
   */
  public static function loadByMachineName($machine_name) {
    $bundles = Bundle::loadAll();
    if (array_key_exists($machine_name, $bundles)) {
      return $bundles[$machine_name];
    }
    return NULL;
  }

  /**
   * Load all bundles.
   *
   * @param bool $reset
   *   Retrive from cache if availabe or not.
   *
   * @return array
   *   An array of Bundles.
   */
  public static function loadAll($reset = FALSE) {
    $bundles = array();
    global $_eck_bundles_cache;
    $cache_enabled = isset($_eck_bundles_cache) ? TRUE : FALSE;
    if ($cache_enabled) {
      if ($reset) {
        $_eck_bundles_cache
          ->reset();
      }
      $bundles = $_eck_bundles_cache
        ->get();
    }
    if (empty($bundles)) {
      $bundles = array();

      // @todo move this to a general function.
      $results = db_select('eck_bundle', 't')
        ->fields('t', array(
        'machine_name',
      ))
        ->execute();
      foreach ($results as $result) {
        $name = $result->machine_name;
        $bundle = new Bundle();
        $bundle
          ->load('machine_name', $name);
        $bundles[$name] = $bundle;
      }
      if ($cache_enabled) {
        $_eck_bundles_cache
          ->set($bundles);
      }
    }
    return $bundles;
  }

  /**
   * Load the bundles of an entity type.
   *
   * @param EntityType $entity_type
   *   An entity type object.
   *
   * @return array
   *   An array with the bundles associated with the given entity type.
   */
  public static function loadByEntityType($entity_type) {
    $entity_bundles =& drupal_static(__FUNCTION__, array());
    $entity_type_name = $entity_type->name;
    if (!isset($entity_bundles[$entity_type_name])) {
      $entity_bundles[$entity_type_name] = array();
      $bundles = Bundle::loadAll();
      foreach ($bundles as $bundle) {
        if ($entity_type_name == $bundle->entity_type) {
          $entity_bundles[$entity_type_name][] = $bundle;
        }
      }
    }
    return isset($entity_bundles[$entity_type_name]) ? $entity_bundles[$entity_type_name] : array();
  }

  /**
   * Adds a field to this bundle.
   *
   * See: @link field Field API data structures @endlink.
   *
   * @param string $field_type
   *   The type of field to add. One of the keys as defined by any field module
   *   using hook_field_info.
   * @param array $options
   *   This is an optional array. Its properties can include:
   *   _use existing_: If TRUE and if a 'field_name' property is specified in
   *   the 'field' property below and the field already exists, then a new
   *   instance will be created using the existing field. All specified 'field
   *   options provided other then the field name will be ignored. If FALSE,
   *   and an existing field is found then a new field_name will be generated.
   *   TRUE by default.
   *   _field_: all options accepted by field_create_field(). Defaults will be
   *   used for each property that is omitted. Most defaults come from
   *   field_create_field(). Default 'field_name'generation.
   *
   * @return bool
   *   The $instance array with the id property filled in as returned by
   *   field_create_instance().
   *
   * @throws \FieldException
   */
  public function addField($field_type, $options = array()) {

    // Check that the field type is known.
    $field_info = field_info_field_types($field_type);
    if (!$field_info) {
      throw new FieldException(t('Attempt to add a field of unknown type %type.', array(
        '%type' => $field_type,
      )));
    }

    // By default use an existing field if one is found.
    $options += array(
      'use existing' => TRUE,
    );

    // Set field options and merge in any provided field settings.
    $field = array(
      'type' => $field_type,
    );
    if (!empty($options['field'])) {
      $field += $options['field'];
    }

    // Retrieve existing fields of this type.
    $field_type_fields = field_read_fields(array(
      'type' => $field_type,
    ), array(
      'include_inactive' => TRUE,
    ));

    // Formulate a default field name.
    if (empty($field['field_name']) || isset($field_type_fields[$field['field_name']]) && !$options['use existing']) {
      $iter = count($field_type_fields) + 1;
      $field += array(
        'field_name' => substr('field_' . $field_type, 0, 28) . '_' . $iter,
      );
    }

    // Create a new field if the field name is unique over active and
    // disabled fields.
    if (!isset($field_type_fields[$field['field_name']])) {
      field_create_field($field);
    }

    // Add an instance of the field to this bundle.
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => $this->entity_type,
      'bundle' => $this->name,
    );

    // Merge any provided properties and settings.
    if (array_key_exists('instance', $options)) {
      $instance += $options['instance'];
    }
    return field_create_instance($instance);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Bundle::addField public function Adds a field to this bundle.
Bundle::createLabel private function Create a label.
Bundle::createMachineName private function Create a machine name.
Bundle::delete public function Delete the entity type. Overrides DBObject::delete
Bundle::loadAll public static function Load all bundles.
Bundle::loadByEntityType public static function Load the bundles of an entity type.
Bundle::loadByMachineName public static function This method returns a bundle object.
Bundle::loadMultipleByMachineName public static function
Bundle::save public function Save the entity type. Overrides DBObject::save
Bundle::__construct public function Constructor. Overrides DBObject::__construct
DBObject::$data private property
DBObject::$isNew public property
DBObject::$position private property
DBObject::$primaryKeys private property
DBObject::$serialize private property
DBObject::$table private property
DBObject::$vars private property
DBObject::current public function From Iterator Interface.
DBObject::key public function From Iterator Interface.
DBObject::load protected function Load.
DBObject::next public function From Iterator Interface.
DBObject::rewind public function From Iterator Interface.
DBObject::valid public function From Iterator Interface.
DBObject::__get public function Magic method.
DBObject::__isset public function Magic method.
DBObject::__set public function Magic method.
DBObject::__unset public function Magic method.