You are here

public function EntityDependencyIterator::hasChildren in Entity Dependency API 7

Returns TRUE if an iterator can be created for the current item in the entities array.

Return value

boolean

File

./EntityDependencyIterator.inc, line 140
Entity Dependency classes.

Class

EntityDependencyIterator
Iterator class which does the heavy lifting for detecting dependencies.

Code

public function hasChildren() {
  $current = $this->current;

  // Don't check for dependencies twice.
  if (!empty($this->current['id']) && !isset($this->checked[$current['type']][$current['id']])) {

    // Load the current entity.
    if (!empty($current['revision_id'])) {
      $entity_info = entity_get_info($current['type']);
      if (!empty($entity_info['entity keys']['revision'])) {
        $conditions = array(
          $entity_info['entity keys']['revision'] => $current['revision_id'],
        );
      }
    }
    else {
      $conditions = array();
    }
    $entities = entity_load($current['type'], array(
      $current['id'],
    ), $conditions);
    $entity = reset($entities);

    // When $entity does not exist (may be the case with references
    // to deleted taxonomy terms), skip and remove it from $this->entites.
    if (!$entity) {
      watchdog('entity_dependency', "Missing entity %id of type %type", array(
        '%id' => $current['id'],
        '%type' => $current['type'],
      ), WATCHDOG_WARNING);

      // We can't do anything useful with the no-existent entity,
      // therfore we just remove it.
      array_shift($this->entities);
      return FALSE;
    }
    $this->dependencies = module_invoke_all('entity_dependencies', $entity, $current['type']);

    //$this->belongings = module_invoke_all('entity_belongings', $entity, $this->entityType);

    // Don't add dependencies that already were checked.
    foreach ($this->dependencies as $key => $dependency) {
      if ($dependency['type'] == $current['type'] && $dependency['id'] == $current['id'] || isset($this->checked[$dependency['type']][$dependency['id']])) {
        unset($this->dependencies[$key]);
      }
      else {
        $this->causes[$dependency['type']][$dependency['id']] = $current;
      }
    }

    // Let other modules have their say.
    drupal_alter('entity_dependencies', $this->dependencies, $entity, $current['type']);

    // Now mark this as checked.
    $this->checked[$current['type']][$current['id']] = TRUE;
    if (!empty($this->dependencies)) {
      return TRUE;
    }
  }
  return FALSE;
}