You are here

function cer_entity_settings_cron in Corresponding Entity References 7.3

Implements hook_cron().

File

extensions/cer_entity_settings/cer_entity_settings.module, line 59

Code

function cer_entity_settings_cron($field = NULL) {

  // Delete defunct instances of the given field. An instance is considered
  // defunct if there are no presets which refer the entity type and bundle
  // on which it's instantiated.
  if (isset($field)) {
    $view = views_get_view('cer_endpoint_in_use');
    $field = field_info_field($field);

    // Loop through every instance of the field, executing the view for each one.
    foreach ($field['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {
        $view
          ->set_exposed_input(array(
          'left' => "{$entity_type}:{$bundle}:",
          'right' => "{$entity_type}:{$bundle}:",
        ));
        $view
          ->execute();
        if (empty($view->result)) {
          $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
          if ($instance) {

            // Do NOT delete the field if this is the last instance of it. We
            // clean up our own fields during uninstall.
            field_delete_instance($instance, FALSE);
          }
        }
      }
    }
  }
  else {
    call_user_func(__FUNCTION__, 'cer_settings');
    call_user_func(__FUNCTION__, 'cer_store_settings');
  }
}