You are here

function entity_modules_disabled in Entity API 7

Implements hook_modules_disabled().

File

./entity.module, line 997

Code

function entity_modules_disabled($modules) {
  foreach (_entity_modules_get_default_types($modules) as $entity_type) {
    $info = entity_get_info($entity_type);

    // Do nothing if the module providing the entity type has been disabled too.
    if (isset($info['module']) && in_array($info['module'], $modules)) {
      return;
    }
    $keys = $info['entity keys'] + array(
      'module' => 'module',
      'status' => 'status',
      'name' => $info['entity keys']['id'],
    );

    // Remove entities provided in code by one of the disabled modules.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type, '=')
      ->propertyCondition($keys['module'], $modules, 'IN')
      ->propertyCondition($keys['status'], array(
      ENTITY_IN_CODE,
      ENTITY_FIXED,
    ), 'IN');
    $result = $query
      ->execute();
    if (isset($result[$entity_type])) {
      $entities = entity_load($entity_type, array_keys($result[$entity_type]));
      entity_delete_multiple($entity_type, array_keys($entities));
    }

    // Update overridden entities to be now custom.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type, '=')
      ->propertyCondition($keys['module'], $modules, 'IN')
      ->propertyCondition($keys['status'], ENTITY_OVERRIDDEN, '=');
    $result = $query
      ->execute();
    if (isset($result[$entity_type])) {
      foreach (entity_load($entity_type, array_keys($result[$entity_type])) as $name => $entity) {
        $entity->{$keys['status']} = ENTITY_CUSTOM;
        $entity->{$keys['module']} = NULL;
        entity_save($entity_type, $entity);
      }
    }

    // Rebuild the remaining defaults so any alterations of the disabled modules
    // are gone.
    _entity_defaults_rebuild($entity_type);
  }
}