You are here

entity_dependency.module in Entity Dependency API 7

Entity Depedendency module functions.

File

entity_dependency.module
View source
<?php

/**
 * @file
 * Entity Depedendency module functions.
 */

/**
 * Include core hook implementations.
 */
module_load_include('inc', 'entity_dependency', 'entity_dependency.core');

/**
 * Factory function for an entity dependency iterator.
 *
 * The $entities array should be structured as below, where all string keys
 * are entity types and the numeric keys are entity ids.
 * @code
 *   $entities = array(
 *     'node' => array(10, 12),
 *     'taxonomy_term' => array(16),
 *   );
 * @endcode
 */
function entity_dependency_iterator($entities, $plugin = 'default_iterator') {
  if (module_exists('ctools')) {
    $class_name = ctools_plugin_load_class('entity_dependency', 'iterator', $plugin, 'handler');
    $iterator = new $class_name($entities);
  }
  else {
    $iterator = new EntityDependencyIterator($entities);
  }
  return new EntityDependencyIteratorIterator($iterator);
}

/**
 * Helper function to add entity dependencies to a dependency array.
 *
 * We never treat user UID 0 or 1 as dependencies. Those are low level user
 * accounts ("anonymous" and "root") that already exists in most systems.
 *
 * @param $dependencies
 *   The dependency array.
 * @param $objects
 *   Array of objects that should be checked for dependencies in $properties.
 * @param $entity_type
 *   The type of entity that $properties will add dependency on.
 * @param $properties
 *   An array of properties that adds dependencies to $objects. All properties
 *   must only point to one entity type at the time.
 *
 * @see entity_dependency.core.inc
 */
function entity_dependency_add(&$dependencies, $objects, $entity_type, $properties) {
  if (!is_array($objects)) {
    $objects = array(
      $objects,
    );
  }
  if (!is_array($properties)) {
    $properties = array(
      $properties,
    );
  }
  foreach ($objects as $object) {
    foreach ($properties as $property) {
      $value = NULL;
      if (is_object($object) && isset($object->{$property})) {
        $value = $object->{$property};
      }
      elseif (is_array($object) && isset($object[$property])) {
        $value = $object[$property];
      }
      if (!empty($value) && !($entity_type == 'user' && ((int) $value == 0 || (int) $value == 1))) {
        $dependencies[] = array(
          'type' => $entity_type,
          'id' => $value,
        );
      }
    }
  }
}

/**
 * Implements hook_ctools_plugin_api().
 */
function entity_dependency_ctools_plugin_api($module, $api) {
  if ($module == 'entity_dependency' && $api == 'iterator') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
function entity_dependency_ctools_plugin_type() {
  $plugins['iterator'] = array(
    'cache' => TRUE,
    'use hooks' => TRUE,
  );
  return $plugins;
}

/**
 * Implements hook_entity_dependency_iterator().
 */
function entity_dependency_entity_dependency_iterator() {
  $plugins = array();
  $plugins['default_iterator'] = array(
    'title' => t('Default Entity Dependency Iterator'),
    'description' => t('Iterate through entities dependencies.'),
    'handler' => 'EntityDependencyIterator',
    'file' => 'EntityDependencyIterator.inc',
  );
  return $plugins;
}

/**
 * Fetches all ctools plugins defined by entity_dependency.
 */
function entity_dependency_get_all_ctools_plugins() {
  ctools_include('plugins');
  $iterators = ctools_get_plugins('entity_dependency', 'iterator');
  $plugins = array();
  foreach ($iterators as $machine_name => $plugin) {
    $plugins[$machine_name] = $plugin['title'];
  }
  return $plugins;
}

Functions

Namesort descending Description
entity_dependency_add Helper function to add entity dependencies to a dependency array.
entity_dependency_ctools_plugin_api Implements hook_ctools_plugin_api().
entity_dependency_ctools_plugin_type Implements hook_ctools_plugin_type().
entity_dependency_entity_dependency_iterator Implements hook_entity_dependency_iterator().
entity_dependency_get_all_ctools_plugins Fetches all ctools plugins defined by entity_dependency.
entity_dependency_iterator Factory function for an entity dependency iterator.