You are here

function conditional_fields_load_dependencies in Conditional Fields 7.3

Same name and namespace in other branches
  1. 8 conditional_fields.module \conditional_fields_load_dependencies()
  2. 4.x conditional_fields.module \conditional_fields_load_dependencies()

Loads all dependencies from the database.

The result can be filtered by providing an entity type and a bundle name.

7 calls to conditional_fields_load_dependencies()
conditional_fields_dependencies_overview_page in includes/conditional_fields.admin.inc
Dependencies administration page.
conditional_fields_dependency_add_form in includes/conditional_fields.admin.inc
Dependency add form.
conditional_fields_element_after_build in ./conditional_fields.module
Processes form elements with dependencies.
conditional_fields_entity_view_alter in ./conditional_fields.module
Implements hook_entity_view_alter().
conditional_fields_features_export_options in includes/conditional_fields.features.inc
Implements hook_features_export_options().

... See full list

File

./conditional_fields.module, line 1277
Define dependencies between fields based on their states and values.

Code

function conditional_fields_load_dependencies($entity_type = NULL, $bundle = NULL) {

  // Use the advanced drupal_static() pattern.
  static $dependencies;
  if (!isset($dependencies)) {
    $dependencies =& drupal_static(__FUNCTION__);
  }
  if (!$dependencies) {
    $dependencies = array();
  }
  if (!isset($dependencies[$entity_type][$bundle])) {
    if (!empty($entity_type) && !empty($bundle)) {
      $dependencies[$entity_type][$bundle] = array();
    }
    $default_options = conditional_fields_dependency_default_options();
    $select = db_select('conditional_fields', 'cf')
      ->fields('cf', array(
      'id',
      'options',
    ))
      ->orderBy('cf.dependent');
    $fci_depende = $select
      ->join('field_config_instance', 'fci_dependee', 'cf.dependee = fci_dependee.id');
    $fci_dependent = $select
      ->join('field_config_instance', 'fci_dependent', 'cf.dependent = fci_dependent.id');
    $select
      ->addField($fci_depende, 'field_name', 'dependee');
    $select
      ->addField($fci_dependent, 'field_name', 'dependent');
    $select
      ->addField($fci_depende, 'entity_type');
    $select
      ->addField($fci_depende, 'bundle');
    if ($entity_type) {
      $select
        ->condition(db_and()
        ->condition('fci_dependee.entity_type', $entity_type)
        ->condition('fci_dependent.entity_type', $entity_type));
    }
    if ($bundle) {
      $select
        ->condition(db_and()
        ->condition('fci_dependee.bundle', $bundle)
        ->condition('fci_dependent.bundle', $bundle));
    }
    $result = $select
      ->execute();
    foreach ($result as $dependency) {
      $result_entity_type = $entity_type ? $entity_type : $dependency->entity_type;
      $result_bundle = $bundle ? $bundle : $dependency->bundle;
      $options = unserialize($dependency->options);
      $options += $default_options;
      $dependencies[$result_entity_type][$result_bundle]['dependents'][$dependency->dependent][$dependency->id] = array(
        'dependee' => $dependency->dependee,
        'options' => $options,
      );
      $dependencies[$result_entity_type][$result_bundle]['dependees'][$dependency->dependee][$dependency->id] = array(
        'dependent' => $dependency->dependent,
        'options' => $options,
      );
    }
  }
  if ($entity_type && isset($dependencies[$entity_type])) {
    if ($bundle && isset($dependencies[$entity_type][$bundle])) {
      return $dependencies[$entity_type][$bundle];
    }
    return $dependencies[$entity_type];
  }
  return $dependencies;
}