You are here

function ddf_entityreference_field_update_field in Dynamic dependent fields 7

Implements hook_field_update_field().

File

modules/ddf_entityreference/ddf_entityreference.module, line 88

Code

function ddf_entityreference_field_update_field($field, $prior_field, $has_data) {
  if ($field['type'] != 'entityreference') {
    return;
  }

  // We need 'bundles' element of the field structure.
  $field = field_info_field($field['field_name']);

  // Remove old dependencies.
  foreach ($field['bundles'] as $entity_type => $bundles) {
    foreach ($bundles as $bundle) {
      ddf_remove_dependency($field['field_name'], $entity_type, $bundle);
    }
  }

  // Add new dependencies.
  if (isset($field['settings']['handler_settings']['view']['args'])) {
    $controlling_fields = array();
    foreach ($field['settings']['handler_settings']['view']['args'] as $arg) {
      $matches = array();
      if (preg_match('/^\\{([^{}]+)\\}$/', $arg, $matches)) {
        $field_name = $matches[1];
        if (!empty($controlling_fields[$field_name])) {

          // Do not try to re-add the same dependency.
          continue;
        }
        $controlling_fields[$field_name] = TRUE;
        foreach ($field['bundles'] as $entity_type => $bundles) {
          foreach ($bundles as $bundle) {
            if (field_info_instance($entity_type, $field_name, $bundle)) {
              ddf_add_dependency($field_name, $field['field_name'], $entity_type, $bundle, array(
                'type' => 'options',
              ));
            }
          }
        }
      }
    }
  }
}