You are here

field_sql_norevisions.module in Field SQL norevisions 7.2

Same filename and directory in other branches
  1. 7 field_sql_norevisions.module

Field SQL norevisions: hooks' implementations and helper functions.

File

field_sql_norevisions.module
View source
<?php

/**
 * @file
 * Field SQL norevisions: hooks' implementations and helper functions.
 */

/**
 * Implements hook_field_storage_info_alter().
 * 
 * We override field_sql_storage's module pointer to point
 * to ourself (field_sql_norevisions) so we can override all relevant functions.
 */
function field_sql_norevisions_field_storage_info_alter(&$info) {
  $info['field_sql_storage']['module'] = 'field_sql_norevisions';
}

/**
 * Implements hook_help().
 */
function field_sql_norevisions_help($path, $arg) {

  // @todo Add some help text on the module settings page.
  switch ($path) {
    case 'admin/help#field_sql_norevisions':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Field SQL norevisions module alters the default Field SQL storage module to make it possible to not save fields revisions data for entities/bundles.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function field_sql_norevisions_menu() {
  $items = array();
  $items['admin/config/system/field_sql_norevisions'] = array(
    'title' => 'Field SQL norevisions',
    'description' => 'Adjust Field SQL norevisions settings.',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'position' => 'left',
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/system/field_sql_norevisions/settings'] = array(
    'title' => 'Settings',
    'description' => 'Enable or disable fields revisions for entities/bundles.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'field_sql_norevisions_entity_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'field_sql_norevisions.admin.inc',
    // Make menu items outputted by system_admin_menu_block_page() but also
    // show as tabs to each other.
    // @todo What is the best way to hide tabs on parent page?
    'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/system/field_sql_norevisions/delete'] = array(
    'title' => 'Delete revisions data',
    'description' => 'Delete fields revisions data for entities/bundles.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'field_sql_norevisions_batch_delete_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'field_sql_norevisions.admin.inc',
    'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function field_sql_norevisions_theme($existing, $type, $theme, $path) {
  return array(
    'field_sql_norevisions_delete_confirmation' => array(
      'variables' => array(
        'settings' => array(),
        'selection' => array(),
      ),
      'file' => 'field_sql_norevisions.theme.inc',
    ),
  );
}

/**
 * Implements hook_field_storage_create_field().
 *
 * @see field_sql_storage_field_storage_create_field()
 */
function field_sql_norevisions_field_storage_create_field($field) {
  return field_sql_storage_field_storage_create_field($field);
}

/**
 * Implements hook_field_update_forbid().
 *
 * Forbid any field update that changes column definitions if there is
 * any data.
 *
 * @see field_sql_storage_field_update_forbid()
 */
function field_sql_norevisions_field_update_forbid($field, $prior_field, $has_data) {
  return field_sql_storage_field_update_forbid($field, $prior_field, $has_data);
}

/**
 * Implements hook_field_storage_update_field().
 *
 * @see field_sql_storage_field_storage_update_field()
 */
function field_sql_norevisions_field_storage_update_field($field, $prior_field, $has_data) {
  return field_sql_storage_field_storage_update_field($field, $prior_field, $has_data);
}

/**
 * Implements hook_field_storage_delete_field().
 *
 * @see field_sql_storage_field_storage_delete_field()
 */
function field_sql_norevisions_field_storage_delete_field($field) {
  return field_sql_storage_field_storage_delete_field($field);
}

/**
 * Implements hook_field_storage_load().
 *
 * @see field_sql_storage_field_storage_load()
 */
function field_sql_norevisions_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  return field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options);
}

/**
 * Implements hook_field_storage_write().
 *
 * @see field_sql_storage_field_storage_write()
 */
function field_sql_norevisions_field_storage_write($entity_type, $entity, $op, $fields) {
  $enabled_entity_bundles = variable_get('field_sql_norevisions_entities', array());
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  if (empty($enabled_entity_bundles[$entity_type][$bundle])) {

    // We fall back to normal Field SQL storage function.
    field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields);
    return;
  }
  if (!isset($vid)) {
    $vid = $id;
  }
  foreach ($fields as $field_id) {
    $field = field_info_field_by_id($field_id);
    $field_name = $field['field_name'];
    $table_name = _field_sql_storage_tablename($field);
    $all_languages = field_available_languages($entity_type, $field);
    $field_languages = array_intersect($all_languages, array_keys((array) $entity->{$field_name}));

    // Delete and insert, rather than update, in case a value was added.
    if ($op == FIELD_STORAGE_UPDATE) {

      // Delete languages present in the incoming $entity->$field_name.
      // Delete all languages if $entity->$field_name is empty.
      $languages = !empty($entity->{$field_name}) ? $field_languages : $all_languages;
      if ($languages) {
        db_delete($table_name)
          ->condition('entity_type', $entity_type)
          ->condition('entity_id', $id)
          ->condition('language', $languages, 'IN')
          ->execute();
      }
    }

    // Prepare the multi-insert query.
    $do_insert = FALSE;
    $columns = array(
      'entity_type',
      'entity_id',
      'revision_id',
      'bundle',
      'delta',
      'language',
    );
    foreach ($field['columns'] as $column => $attributes) {
      $columns[] = _field_sql_storage_columnname($field_name, $column);
    }
    $query = db_insert($table_name)
      ->fields($columns);
    foreach ($field_languages as $langcode) {
      $items = (array) $entity->{$field_name}[$langcode];
      $delta_count = 0;
      foreach ($items as $delta => $item) {

        // We now know we have someting to insert.
        $do_insert = TRUE;
        $record = array(
          'entity_type' => $entity_type,
          'entity_id' => $id,
          'revision_id' => $vid,
          'bundle' => $bundle,
          'delta' => $delta,
          'language' => $langcode,
        );
        foreach ($field['columns'] as $column => $attributes) {
          $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
        }
        $query
          ->values($record);
        if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
          break;
        }
      }
    }

    // Execute the query if we have values to insert.
    if ($do_insert) {
      $query
        ->execute();
    }
  }
}

/**
 * Implements hook_field_storage_delete().
 *
 * This function deletes data for all fields for an entity from the database.
 *
 * @see field_sql_storage_field_storage_delete()
 */
function field_sql_norevisions_field_storage_delete($entity_type, $entity, $fields) {
  field_sql_storage_field_storage_delete($entity_type, $entity, $fields);
}

/**
 * Implements hook_field_storage_purge().
 *
 * This function deletes data from the database for a single field on
 * an entity.
 *
 * @see field_sql_storage_field_storage_purge()
 */
function field_sql_norevisions_field_storage_purge($entity_type, $entity, $field, $instance) {
  field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
}

/**
 * Implements hook_field_storage_query().
 *
 * @see field_sql_storage_field_storage_query()
 */
function field_sql_norevisions_field_storage_query(EntityFieldQuery $query) {
  return field_sql_storage_field_storage_query($query);
}

/**
 * Implements hook_field_storage_delete_revision().
 *
 * This function actually deletes the data from the database.
 *
 * @see field_sql_storage_field_storage_delete_revision()
 */
function field_sql_norevisions_field_storage_delete_revision($entity_type, $entity, $fields) {
  field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields);
}

/**
 * Implements hook_field_storage_delete_instance().
 *
 * This function simply marks for deletion all data associated with the field.
 *
 * @see field_sql_storage_field_storage_delete_instance()
 */
function field_sql_norevisions_field_storage_delete_instance($instance) {
  field_sql_storage_field_storage_delete_instance($instance);
}

/**
 * Implements hook_field_attach_rename_bundle().
 *
 * @see field_sql_storage_field_attach_rename_bundle()
 */
function field_sql_norevisions_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new);
}

/**
 * Implements hook_field_storage_purge_field().
 *
 * All field data items and instances have already been purged, so all
 * that is left is to delete the table.
 *
 * @see field_sql_storage_field_storage_purge_field()
 */
function field_sql_norevisions_field_storage_purge_field($field) {
  return field_sql_storage_field_storage_purge_field($field);
}

/**
 * Implements hook_field_storage_details().
 *
 * @see field_sql_storage_field_storage_details()
 */
function field_sql_norevisions_field_storage_details($field) {
  return field_sql_storage_field_storage_details($field);
}

/**
 * Form #process callback.
 *
 * Adds some common elements to the module's admin forms.
 */
function _field_sql_norevisions_admin_form_process($element, &$form_state) {
  $module_path = drupal_get_path('module', 'field_sql_norevisions');
  $element['#attributes']['class'][] = 'field-sql-norevisions-admin-form';
  $element['#attached']['css'][] = $module_path . '/field_sql_norevisions.admin.css';
  $element['#attached']['js'][] = $module_path . '/field_sql_norevisions.admin.js';
  return $element;
}

Functions

Namesort descending Description
field_sql_norevisions_field_attach_rename_bundle Implements hook_field_attach_rename_bundle().
field_sql_norevisions_field_storage_create_field Implements hook_field_storage_create_field().
field_sql_norevisions_field_storage_delete Implements hook_field_storage_delete().
field_sql_norevisions_field_storage_delete_field Implements hook_field_storage_delete_field().
field_sql_norevisions_field_storage_delete_instance Implements hook_field_storage_delete_instance().
field_sql_norevisions_field_storage_delete_revision Implements hook_field_storage_delete_revision().
field_sql_norevisions_field_storage_details Implements hook_field_storage_details().
field_sql_norevisions_field_storage_info_alter Implements hook_field_storage_info_alter().
field_sql_norevisions_field_storage_load Implements hook_field_storage_load().
field_sql_norevisions_field_storage_purge Implements hook_field_storage_purge().
field_sql_norevisions_field_storage_purge_field Implements hook_field_storage_purge_field().
field_sql_norevisions_field_storage_query Implements hook_field_storage_query().
field_sql_norevisions_field_storage_update_field Implements hook_field_storage_update_field().
field_sql_norevisions_field_storage_write Implements hook_field_storage_write().
field_sql_norevisions_field_update_forbid Implements hook_field_update_forbid().
field_sql_norevisions_help Implements hook_help().
field_sql_norevisions_menu Implements hook_menu().
field_sql_norevisions_theme Implements hook_theme().
_field_sql_norevisions_admin_form_process Form #process callback.