You are here

data_entity.module in Data 7

data_entity.module TODO: Enter file description here.

File

data_entity/data_entity.module
View source
<?php

/**
 * @file data_entity.module
 * TODO: Enter file description here.
 */

/**
 * Get all data tables which are declared as entities.
 *
 * @return
 *  An array of table objects, keyed by table name, for the tables that are
 *  declared as entity types.
 */
function data_entity_get_entity_tables($reset = FALSE) {
  $tables = data_get_all_tables($reset);
  $entity_tables = array();
  foreach ($tables as $name => $table) {
    $meta = $table
      ->get('meta');
    if (!empty($meta['is_entity_type'])) {
      $entity_tables[$name] = $table;
    }
  }
  return $entity_tables;
}

/**
 * Adds our default values to a table's meta information.
 */
function data_entity_meta_add_defaults(&$meta) {
  foreach ($meta['fields'] as $field => $data) {
    $meta['fields'][$field] += array(
      'locked' => FALSE,
      'required' => FALSE,
    );
    $meta += array(
      'label_field' => '',
    );
  }
  $meta += array(
    'is_entity_type' => FALSE,
    'entity_id' => '',
  );
}

/**
 * Implements hook_entity_info().
 *
 * Declare every data table as an entity.
 *
 * @todo Add an admin UI to request tables for this rather than do all.
 */
function data_entity_entity_info() {
  $tables = data_entity_get_entity_tables();
  $info = array();
  foreach ($tables as $table_name => $table) {
    data_entity_meta_add_defaults($table->meta);
    $meta = $table
      ->get('meta');
    if (empty($meta['is_entity_type'])) {
      continue;
    }
    $entity_type = 'data_' . $table_name;
    $info[$entity_type] = array(
      'label' => $table->title,
      'controller class' => 'DataEntityController',
      'base table' => $table_name,
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => $meta['entity_id'],
      ),
      'bundles' => array(
        $entity_type => array(
          'label' => $table->title,
          'admin' => array(
            'path' => 'admin/structure/data/edit/' . $table_name,
            'access arguments' => array(
              'administer data tables',
            ),
          ),
        ),
      ),
      'view modes' => array(),
      'uri callback' => 'data_entity_uri',
      // Entity API properties.
      'module' => 'data_entity',
      'views controller class' => FALSE,
    );

    // Use label if specified
    if (isset($table->meta['label_field'])) {
      $info[$entity_type]['entity keys']['label'] = $table->meta['label_field'];
    }
    else {
      $info[$entity_type]['label callback'] = 'data_entity_label';
    }
  }
  return $info;
}

/**
 * Implements hook_menu().
 */
function data_entity_menu() {
  $tables = data_get_all_tables();
  foreach ($tables as $table_name => $table) {

    // Items that are in the same tab set as Field UI tabs need to have separate
    // menu router items.
    $items['admin/structure/data/edit/' . $table_name . '/entity-type'] = array(
      'title' => 'Entity type',
      'description' => 'Configure the entity type for the data table.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'data_entity_admin_entity_type_form',
        4,
      ),
      'file' => 'data_entity.admin.inc',
      'access arguments' => array(
        'administer data tables',
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => -3,
    );

    // The following menu items only concern tables that are declared as
    // entity types, so skip those that are not.
    $meta = $table
      ->get('meta');
    if (empty($meta['is_entity_type'])) {
      continue;
    }
    $items['admin/structure/data/edit/' . $table_name . '/entity-form'] = array(
      'title' => 'Configure entity form',
      'description' => 'Administer data tables.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'data_entity_admin_entity_form',
        4,
      ),
      'file' => 'data_entity.admin.inc',
      'access arguments' => array(
        'administer data tables',
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => -2,
    );
  }
  $items['admin/content/data/entity/%data_ui_table/%data_entity_item'] = array(
    'title' => 'Edit data item',
    'load arguments' => array(
      4,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'data_entity_entity_edit_form',
      4,
      5,
    ),
    'file' => 'data_entity.pages.inc',
    'access callback' => 'data_entity_table_menu_access',
    'access arguments' => array(
      4,
    ),
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function data_entity_menu_alter(&$items) {

  // Attempt at fixing tab parentage for Field UI items.
  // @see data_entity_entity_info() for more details.
  $tables = data_entity_get_entity_tables();
  $info = array();

  /*
  // This breaks access on these menu items!
  foreach ($tables as $table_name => $table) {
    $field_ui_base_path = "admin/structure/data/edit/$table_name";
    foreach (array('fields', 'fields/%', 'display') as $path_suffix) {
      $items[$field_ui_base_path . '/' . $path_suffix]['tab_parent']  = 'admin/structure/data/edit/%';
      $items[$field_ui_base_path . '/' . $path_suffix]['tab_root']    = 'admin/structure/data/edit/%';
    }
  }
  */
}

/**
 * Menu access callback.
 */
function data_entity_table_menu_access($table) {
  return user_access('edit data in table ' . $table->name);
}

/**
 * Menu loader callback.
 *
 * Called 'data_entity_item_load' to avoid being data's hook_entity_load()!
 */
function data_entity_item_load($deid, $table_name) {
  $entity_type = 'data_' . $table_name;
  $data_entity = entity_load($entity_type, array(
    $deid,
  ));
  return $data_entity ? reset($data_entity) : FALSE;
}

/**
 * Implements hook_permission().
 */
function data_entity_permission() {
  $tables = data_entity_get_entity_tables();
  $permissions = array();
  foreach ($tables as $table_name => $table) {
    $permissions['edit data in table ' . $table_name] = array(
      'title' => t('Edit data in the %table_name table', array(
        '%table_name' => $table->title,
      )),
    );
  }
  return $permissions;
}

/**
 * Entity Label callback.
 */
function data_entity_label($entity, $entity_type) {
  return t('No label');
}

/**
 * Entity URI callback.
 */
function data_entity_uri($data_entity) {
  $entity_type = $data_entity->entity_type;
  list($id, ) = entity_extract_ids($entity_type, $data_entity);
  return array(
    'path' => 'admin/content/data/entity/' . $data_entity->data_table . '/' . $id,
  );
}

/**
 * Implements hook_theme().
 */
function data_entity_theme($existing, $type, $theme, $path) {
  return array(
    'data_entity_admin_entity_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function data_entity_views_api() {
  return array(
    'api' => '3.0-alpha1',
    'path' => drupal_get_path('module', 'data_entity') . '/views',
  );
}

/**
 * Implements hook_feeds_processor_targets_alter().
 *
 * @see data_entity_feed_unique_callback()
 */
function data_entity_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {

  // Get the information about this entity.
  $entity_info = entity_get_info($entity_type);

  // See if this is a Data entity.
  if (isset($entity_info['module']) && $entity_info['module'] == 'data_entity') {

    // Add a unique callback for each target field.
    foreach ($targets as &$target) {
      $target['unique_callbacks'][] = 'data_entity_feed_unique_callback';
      $target['bundle_name'] = $bundle_name;
      $target['optional_unique'] = TRUE;
    }
  }
}

/**
 * Feeds unique callback for Data table field targets.
 *
 * @param FeedsSource $source
 *   The Feed source.
 * @param string $entity_type
 *   Entity type for the entity to be processed.
 * @param string $bundle
 *   Bundle name for the entity to be processed.
 * @param string $target
 *   A string identifying the unique target on the entity.
 * @param array $values
 *   The unique values to be checked.
 *
 * @return int
 *   The existing entity id, or NULL if nothing is found.
 *
 * @see data_entity_feeds_processor_targets_alter().
 * @see FeedsProcessor::existingEntityId()
 */
function data_entity_feed_unique_callback(FeedsSource $source, $entity_type, $bundle, $target, $values) {

  // Get the information about this entity.
  $entity_info = entity_get_info($entity_type);

  // Extract the entity ID key.
  $entity_id_key = $entity_info['entity keys']['id'];

  // Attempt to load the table.
  if ($table = data_get_table($entity_info['base table'])) {

    // Get the single value out of the array. Not sure why it has to be an array
    // but as we deal in database columns, we know it's always a single value.
    $value = array_pop($values);

    // Check if this value already exists in this table.
    if ($record = $table
      ->handler()
      ->load(array(
      $target => $value,
    ))) {

      // Use the first record.
      $record = reset($record);

      // Determine the primary key value.
      if (isset($record[$entity_id_key])) {
        return $record[$entity_id_key];
      }
    }
  }

  // Nothing found.
}

Functions

Namesort descending Description
data_entity_entity_info Implements hook_entity_info().
data_entity_feeds_processor_targets_alter Implements hook_feeds_processor_targets_alter().
data_entity_feed_unique_callback Feeds unique callback for Data table field targets.
data_entity_get_entity_tables Get all data tables which are declared as entities.
data_entity_item_load Menu loader callback.
data_entity_label Entity Label callback.
data_entity_menu Implements hook_menu().
data_entity_menu_alter Implements hook_menu_alter().
data_entity_meta_add_defaults Adds our default values to a table's meta information.
data_entity_permission Implements hook_permission().
data_entity_table_menu_access Menu access callback.
data_entity_theme Implements hook_theme().
data_entity_uri Entity URI callback.
data_entity_views_api Implements hook_views_api().