You are here

function domain_entity_types_enable_domain_field in Domain Access Entity 7

Helper function to active domain field on bundles of entity types.

Parameters

array $entity_types: List of Entity types, that list her bundles.

1 call to domain_entity_types_enable_domain_field()
domain_entity_ui_submit in ./domain_entity.admin.inc
Submit handler for the domain entity form.

File

./domain_entity.module, line 769
Defines field (e.g. domain_entity) for entities, and access query alter.

Code

function domain_entity_types_enable_domain_field($entity_types) {

  // Common settings.
  $field_type = 'domain_entity';
  $label = t('Domain');
  $required = TRUE;
  $entities_to_update = array();

  // Reset the entity info, before enabling domain access on entity.
  // Some entities are not fieldable, but this module force it to.
  entity_info_cache_clear();

  // Create fields instance.
  foreach ($entity_types as $entity_type => $bundles) {
    $field_name = domain_entity_get_entity_field_name($entity_type);
    $entity_info = entity_get_info($entity_type);
    foreach ($bundles as $bundle => $widget) {

      // Look for or add the specified field to the requested entity bundle.
      $instance = field_info_instance($entity_type, $field_name, $bundle);
      if (empty($instance)) {
        domain_entity_create_field_instance($field_name, $field_type, $required, $entity_type, $bundle, $label, key($widget));
        drupal_set_message(t("Domain Access behaviour '@widget' has been enabled on the bundle @type of @entity entity type", array(
          '@widget' => key($widget),
          '@type' => $bundle,
          '@entity' => $entity_type,
        )));
      }
    }

    // Query to look at unafilliated entities.
    // EntityFieldQuery excludes entities with empty field values from results
    // when using fieldCondition(), so we look for the difference between
    // results for all entities and those that are already affiliated to
    // domains.
    $entity_key = $entity_info['entity keys']['id'];

    // Query for all entities in the type.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type);
    $result = $query
      ->execute();
    if (!empty($result[$entity_type])) {
      $all_entities = $result[$entity_type];

      // Query for entities affiliated to domains.
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', $entity_type);
      $query
        ->fieldCondition($field_name);
      $result = $query
        ->execute();
      $affiliated_entities = empty($result[$entity_type]) ? array() : $result[$entity_type];

      // Find unafilliated entities by taking the differernce.
      $unaffiliated_entities = array_diff_key($all_entities, $affiliated_entities);
      if (!empty($unaffiliated_entities)) {
        $entities_to_update[$entity_type] = array();
        foreach ($unaffiliated_entities as $key => $value) {
          $id = $value->{$entity_key};
          $entities_to_update[$entity_type][] = $id;
        }
      }
    }
  }

  // Clear cache as we are going to update existing entities field values.
  field_cache_clear();

  // Create a batch operation to update entities that are not affiliated to a domain.
  $operations = array();
  foreach ($entities_to_update as $entity_type => $entities) {
    foreach ($entities as $entity_id) {
      $operations[] = array(
        '_domain_entity_update_entity_defaut_value',
        array(
          $entity_type,
          $entity_id,
        ),
      );
    }
  }
  $batch = array(
    'operations' => $operations,
    'title' => t('Update existing entities domain affiliation default values'),
    'error_message' => st('The domain access update has encountered an error.'),
    'finished' => '_domain_entity_update_entity_defaut_value_finished',
  );
  if (count($operations)) {

    // Bypass access restriction to allow the current user,
    // to access the entity that are not already assigned.
    $domain_entity_allowed_entity_types_backup = variable_get('domain_entity_allowed_entity_types', array());
    variable_set('domain_entity_bypass_access_conditions_backup', variable_get('domain_entity_bypass_access_conditions', FALSE));
    variable_set('domain_entity_bypass_access_conditions', TRUE);
    batch_set($batch);
  }
  else {
    drupal_set_message(t('0 entities updated.'));
  }
}