updated modules
views friendly_register serial address_field i18n
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Internal functions for the Serial module.
|
||||
@@ -16,94 +15,94 @@
|
||||
/**
|
||||
* Creates an assistant serial table for a new created field.
|
||||
*
|
||||
* @param $field
|
||||
* a serial field
|
||||
* @param $instance
|
||||
* a new instance of that serial field
|
||||
* @param array $field
|
||||
* Serial field.
|
||||
* @param array $instance
|
||||
* New instance of that serial field.
|
||||
*/
|
||||
function _serial_create_table($field, $instance) {
|
||||
function _serial_create_table(array $field, array $instance) {
|
||||
$table = _serial_get_field_table_name($field, $instance);
|
||||
$schema = _serial_get_table_schema();
|
||||
db_create_table($table, $schema);
|
||||
|
||||
if (!db_table_exists($table)) {
|
||||
db_create_table($table, _serial_get_table_schema());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops an assistant serial table for a deleted field.
|
||||
*
|
||||
* @param $field
|
||||
* a serial field
|
||||
* @param $instance
|
||||
* a deleted instance of that serial field
|
||||
* @param array $field
|
||||
* Serial field.
|
||||
* @param array $instance
|
||||
* Deleted instance of that serial field.
|
||||
*/
|
||||
function _serial_drop_table($field, $instance) {
|
||||
$table = _serial_get_field_table_name($field, $instance);
|
||||
db_drop_table($table);
|
||||
function _serial_drop_table(array $field, array $instance) {
|
||||
db_drop_table(_serial_get_field_table_name($field, $instance));
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames serial table(s) when a content type us renamed.
|
||||
*
|
||||
* @param $old_type
|
||||
* an old node type machine name
|
||||
* @param $new_type
|
||||
* a new node type machine name
|
||||
* @param string $old_type
|
||||
* An old node type machine name.
|
||||
* @param string $new_type
|
||||
* A new node type machine name.
|
||||
*/
|
||||
function _serial_rename_tables($old_type, $new_type) {
|
||||
|
||||
// Build the query to find all affected tables.
|
||||
$query = db_select('field_config', 'f')->fields('f', array('field_name'));
|
||||
$table_joined_alias = $query->join(
|
||||
'field_config_instance', 'i',
|
||||
'(f.field_name = i.field_name) AND ' .
|
||||
'(f.type = :field_type) AND (i.bundle = :bundle_type)',
|
||||
array(':field_type' => 'serial', ':bundle_type' => $new_type)
|
||||
$query = db_select('field_config', 'f')
|
||||
->fields('f', array('field_name'));
|
||||
|
||||
$query->join(
|
||||
'field_config_instance',
|
||||
'i',
|
||||
"f.field_name = i.field_name AND f.type = 'serial' AND i.bundle = '$new_type'"
|
||||
);
|
||||
|
||||
// Add an access check and execute it.
|
||||
$result = $query->addTag('node_access')->execute();
|
||||
|
||||
// Rename each affected table.
|
||||
foreach ($result as $record) {
|
||||
$old_table = _serial_get_table_name($old_type, $record->field_name);
|
||||
$new_table = _serial_get_table_name($new_type, $record->field_name);
|
||||
db_rename_table($old_table, $new_table);
|
||||
foreach ($query->addTag('node_access')->execute() as $record) {
|
||||
db_rename_table(
|
||||
_serial_get_table_name($old_type, $record->field_name),
|
||||
_serial_get_table_name($new_type, $record->field_name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the assistant table for a specific field.
|
||||
*
|
||||
* @param $field
|
||||
* a serial field
|
||||
* @param $instance
|
||||
* an instance of that serial field
|
||||
* @return
|
||||
* the name of the assistant table of the specified field instance.
|
||||
* @param array $field
|
||||
* Serial field.
|
||||
* @param array $instance
|
||||
* An instance of that serial field.
|
||||
*
|
||||
* @return string
|
||||
* The name of the assistant table of the specified field instance.
|
||||
*/
|
||||
function _serial_get_field_table_name($field, $instance) {
|
||||
function _serial_get_field_table_name(array $field, array $instance) {
|
||||
return _serial_get_table_name($instance['bundle'], $field['field_name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the assistant table for a specific field.
|
||||
*
|
||||
* @param $bundle
|
||||
* the name of the entity type that contains the field
|
||||
* @param $field_name
|
||||
* the name of the field
|
||||
* @return
|
||||
* @param string $bundle
|
||||
* The name of the entity type that contains the field.
|
||||
* @param string $field_name
|
||||
* The name of the field.
|
||||
*
|
||||
* @return string
|
||||
* the name of the assistant table of the specified field.
|
||||
*/
|
||||
function _serial_get_table_name($bundle, $field_name) {
|
||||
return db_escape_table( // be on the safe side
|
||||
'serial_' . $bundle . '_' . $field_name);
|
||||
return db_escape_table('serial_' . $bundle . '_' . $field_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the schema of the assistant tables for generating serial values.
|
||||
*
|
||||
* @return
|
||||
* the assistant table schema.
|
||||
* @return array
|
||||
* Assistant table schema.
|
||||
*/
|
||||
function _serial_get_table_schema() {
|
||||
return array(
|
||||
@@ -131,28 +130,24 @@ function _serial_get_table_schema() {
|
||||
/**
|
||||
* Generates a unique serial value (unique per node type).
|
||||
*
|
||||
* @param $nid
|
||||
* id of the node for which to generate a serial value
|
||||
* @param $bundle
|
||||
* a containing bundle (e.g. content type)
|
||||
* @param $field_name
|
||||
* the field name
|
||||
* @param $delete
|
||||
* indicates if temporary records should be deleted
|
||||
* @return
|
||||
* @param string $bundle
|
||||
* Containing bundle (e.g. content type).
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param bool $delete
|
||||
* Indicates if temporary records should be deleted.
|
||||
*
|
||||
* @return int
|
||||
* the unique serial value number.
|
||||
*/
|
||||
function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
|
||||
|
||||
// Get the name of the relevant table.
|
||||
$table = _serial_get_table_name($bundle, $field_name);
|
||||
|
||||
// Insert a temporary record to get a new unique serial value.
|
||||
$uniqid = uniqid('', TRUE);
|
||||
$sid = db_insert($table)
|
||||
->fields(array(
|
||||
'uniqid' => $uniqid,
|
||||
))
|
||||
->fields(array('uniqid' => $uniqid))
|
||||
->execute();
|
||||
|
||||
// If there's a reason why it's come back undefined, reset it.
|
||||
@@ -160,9 +155,9 @@ function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
|
||||
|
||||
// Delete the temporary record.
|
||||
if ($delete && ($sid % 10) == 0) {
|
||||
db_delete($table)
|
||||
->condition('uniqid', $uniqid, '=')
|
||||
->execute();
|
||||
db_delete($table)
|
||||
->condition('uniqid', $uniqid)
|
||||
->execute();
|
||||
}
|
||||
|
||||
// Return the new unique serial value.
|
||||
@@ -172,46 +167,49 @@ function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
|
||||
/**
|
||||
* Initializes the value of a new serial field in existing nodes.
|
||||
*
|
||||
* @param $bundle
|
||||
* a containing bundle (e.g. content type)
|
||||
* @param $field_name
|
||||
* the field name
|
||||
* @return
|
||||
* the number of existing nodes that have been initialized.
|
||||
* @todo Currently works only for nodes - should support comments and users.
|
||||
*
|
||||
* @param string $bundle
|
||||
* Containing bundle (e.g. content type).
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
*
|
||||
* @return int
|
||||
* Number of existing nodes that have been initialized.
|
||||
*/
|
||||
function _serial_init_old_nodes($bundle, $field_name) {
|
||||
// Retrieve all the node ids of that type:
|
||||
$query = "SELECT nid FROM {node} WHERE type = :type ORDER BY nid";
|
||||
// TODO: Currently works only for nodes - should support comments and users.
|
||||
$result = db_query($query, array('type' => $bundle));
|
||||
$nodes = node_load_multiple(array(), array('type' => $bundle));
|
||||
|
||||
// Allocate a serial number for every old node.
|
||||
foreach ($nodes as $node) {
|
||||
$node->{$field_name} = array(
|
||||
LANGUAGE_NONE => array(
|
||||
array(
|
||||
'value' => _serial_generate_value($bundle, $field_name, FALSE),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Allocate a serial number for every old node:
|
||||
$count = 0;
|
||||
foreach ($result as $node) {
|
||||
$nid = $node->nid;
|
||||
$node = node_load($nid);
|
||||
$sid = _serial_generate_value($bundle, $field_name, FALSE);
|
||||
$node->{$field_name} = array('und' => array(array('value' => $sid)));
|
||||
node_save($node);
|
||||
$count++;
|
||||
}
|
||||
|
||||
// Return the number of existing nodes that have been initialized:
|
||||
return $count;
|
||||
// Return the number of existing nodes that have been initialized.
|
||||
return count($nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all the managed serial fields.
|
||||
*
|
||||
* @return result set containing pairs of (node type name, field name).
|
||||
* @return array
|
||||
* Pairs of node type name, field name.
|
||||
*/
|
||||
function _serial_get_all_fields() {
|
||||
$query = db_select('field_config', 'f');
|
||||
$query->join('field_config_instance', 'i', 'i.field_name = f.field_name');
|
||||
$query->fields('i', array('bundle', 'field_name'))
|
||||
->condition('f.type', 'serial', '=')
|
||||
->condition('i.deleted', 0, '=');
|
||||
$result = $query->execute();
|
||||
return $result->fetchAll();
|
||||
}
|
||||
|
||||
return $query->fields('i', array('bundle', 'field_name'))
|
||||
->condition('f.type', 'serial')
|
||||
->condition('i.deleted', 0)
|
||||
->execute()
|
||||
->fetchAll();
|
||||
}
|
||||
|
@@ -6,9 +6,9 @@ dependencies[] = field
|
||||
files[] = serial.module
|
||||
|
||||
|
||||
; Information added by drupal.org packaging script on 2013-10-15
|
||||
version = "7.x-1.3"
|
||||
; Information added by Drupal.org packaging script on 2015-05-19
|
||||
version = "7.x-1.4"
|
||||
core = "7.x"
|
||||
project = "serial"
|
||||
datestamp = "1381844527"
|
||||
datestamp = "1432026177"
|
||||
|
||||
|
@@ -1,24 +1,15 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update and uninstall functions for the Serial module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function serial_uninstall() {
|
||||
/*
|
||||
* Schema tables are now dropped automatically. However, if any work needs
|
||||
* to be done before this, do it here.
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_schema().
|
||||
*/
|
||||
function serial_field_schema($field) {
|
||||
$columns = array();
|
||||
|
||||
switch ($field['type']) {
|
||||
case 'serial':
|
||||
$columns['value'] = array(
|
||||
@@ -31,9 +22,8 @@ function serial_field_schema($field) {
|
||||
);
|
||||
break;
|
||||
}
|
||||
return array(
|
||||
'columns' => $columns
|
||||
);
|
||||
|
||||
return array('columns' => $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,45 +32,40 @@ function serial_field_schema($field) {
|
||||
function serial_schema() {
|
||||
// Get the standard schema:
|
||||
module_load_include('inc', 'serial');
|
||||
$table_schema = _serial_get_table_schema();
|
||||
|
||||
// Build the schema by iteration over all the serial field instances:
|
||||
$table_schema = _serial_get_table_schema();
|
||||
$schema = array();
|
||||
$result = _serial_get_all_fields();
|
||||
foreach ($result as $field) {
|
||||
$table = _serial_get_table_name($field->bundle, $field->field_name);
|
||||
$schema[$table] = $table_schema;
|
||||
|
||||
foreach (_serial_get_all_fields() as $field) {
|
||||
$schema[_serial_get_table_name($field->bundle, $field->field_name)] = $table_schema;
|
||||
}
|
||||
|
||||
// Return the schema of all the assistant tables (one per serial field instance):
|
||||
// Return the schema of all the assistant tables (one per field instance).
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade path.
|
||||
*
|
||||
* Switches from nids to uniqid.
|
||||
* Switches from nids to uniqids.
|
||||
*/
|
||||
function serial_update_7130() {
|
||||
// Get the new field schema.
|
||||
module_load_include('inc', 'serial');
|
||||
|
||||
$table_schema = _serial_get_table_schema();
|
||||
$uniqid_schema = $table_schema['fields']['uniqid'];
|
||||
|
||||
// Update the schema of old assistant tables.
|
||||
$result = _serial_get_all_fields();
|
||||
foreach ($result as $field) {
|
||||
foreach (_serial_get_all_fields() as $field) {
|
||||
// Empty the table.
|
||||
$table = _serial_get_table_name($field->bundle, $field->field_name);
|
||||
db_delete($table)->execute();
|
||||
|
||||
// Drop nid field and key
|
||||
// Drop nid field and key.
|
||||
db_drop_field($table, 'nid');
|
||||
db_drop_unique_key($table, 'nid');
|
||||
|
||||
// Add uniqid field and key
|
||||
db_add_field($table, 'uniqid', $uniqid_schema);
|
||||
// Add uniqid field and key.
|
||||
db_add_field($table, 'uniqid', $table_schema['fields']['uniqid']);
|
||||
db_add_unique_key($table, 'uniqid', array('uniqid'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,14 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* The Serial module main file.
|
||||
*/
|
||||
|
||||
//==================//
|
||||
// Field Definition //
|
||||
//==================//
|
||||
|
||||
/**
|
||||
* Implements hook_field_info().
|
||||
*/
|
||||
@@ -17,6 +12,9 @@ function serial_field_info() {
|
||||
'serial' => array(
|
||||
'label' => t('Serial'),
|
||||
'description' => t('Auto increment serial field type.'),
|
||||
// The "property_type" should be defined for accessing the
|
||||
// field by entity metadata wrapper.
|
||||
'property_type' => 'serial',
|
||||
'default_widget' => 'serial',
|
||||
'default_formatter' => 'serial_formatter_default',
|
||||
),
|
||||
@@ -28,18 +26,19 @@ function serial_field_info() {
|
||||
*/
|
||||
function serial_field_create_instance($instance) {
|
||||
$field = field_read_field($instance['field_name']);
|
||||
if ($field['type'] == 'serial') {
|
||||
|
||||
if ('serial' == $field['type']) {
|
||||
// Create the assistant table:
|
||||
module_load_include('inc', 'serial');
|
||||
_serial_create_table($field, $instance);
|
||||
|
||||
// Set serial values for old objects
|
||||
// Set serial values for old objects.
|
||||
$old_count = _serial_init_old_nodes($instance['bundle'], $field['field_name']);
|
||||
if ($old_count) {
|
||||
drupal_set_message(
|
||||
t('Serial values have been automatically set for %count existing nodes.',
|
||||
array('%count' => $old_count))
|
||||
);
|
||||
|
||||
if ($old_count > 0) {
|
||||
drupal_set_message(t('Serial values have been automatically set for %count existing nodes.', array(
|
||||
'%count' => $old_count,
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,8 +48,9 @@ function serial_field_create_instance($instance) {
|
||||
*/
|
||||
function serial_field_delete_instance($instance) {
|
||||
$field = field_read_field($instance['field_name']);
|
||||
if ($field['type'] == 'serial') {
|
||||
// Drop the assistant table:
|
||||
|
||||
if ('serial' == $field['type']) {
|
||||
// Drop the assistant table.
|
||||
module_load_include('inc', 'serial');
|
||||
_serial_drop_table($field, $instance);
|
||||
}
|
||||
@@ -60,18 +60,12 @@ function serial_field_delete_instance($instance) {
|
||||
* Implements hook_form_alter().
|
||||
*/
|
||||
function serial_form_alter(&$form, $form_state, $form_id) {
|
||||
if ('field_ui_field_settings_form' == $form_id && 'serial' == $form['field']['type']['#value']) {
|
||||
drupal_set_message(t('Serial field %field has been created.', array(
|
||||
'%field' => $form['field']['field_name']['#value'],
|
||||
)));
|
||||
|
||||
if ($form_id == 'field_ui_field_settings_form' && $form['field']['type']['#value'] == 'serial') {
|
||||
// Show messages:
|
||||
$field_name = $form['field']['field_name']['#value'];
|
||||
drupal_set_message(
|
||||
t('Serial field %field has been created.',
|
||||
array('%field' => $field_name))
|
||||
);
|
||||
|
||||
// Go back to Managed Fields:
|
||||
$type = $form['#bundle'];
|
||||
drupal_goto("admin/structure/types/manage/$type/fields");
|
||||
drupal_goto("admin/structure/types/manage/{$form['#bundle']}/fields");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,19 +73,19 @@ function serial_form_alter(&$form, $form_state, $form_id) {
|
||||
* Implements hook_field_presave().
|
||||
*/
|
||||
function serial_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
||||
module_load_include('inc', 'serial');
|
||||
if (empty($items)) {
|
||||
$sid = _serial_generate_value($instance['bundle'], $field['field_name']);
|
||||
$items = array(array('value' => $sid));
|
||||
module_load_include('inc', 'serial');
|
||||
|
||||
$items = array(array('value' => _serial_generate_value($instance['bundle'], $field['field_name'])));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_field_is_empty().
|
||||
*/
|
||||
function serial_field_is_empty($item, $field) {
|
||||
return FALSE; // never should be treated as empty
|
||||
// Never should be treated as empty.
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,39 +110,6 @@ function serial_node_type_update($info) {
|
||||
}
|
||||
}
|
||||
|
||||
// Tokens for fields are currently not supported - http://drupal.org/node/691078.
|
||||
|
||||
///**
|
||||
// * Implements hook_token_info().
|
||||
// */
|
||||
//function serial_token_info() {
|
||||
// $type = array(
|
||||
// 'name' => t('Nodes'),
|
||||
// 'description' => t('Tokens related to individual nodes.'),
|
||||
// 'needs-data' => 'node',
|
||||
// );
|
||||
// $node['serial'] = array(
|
||||
// 'name' => t("Serial Field"),
|
||||
// 'description' => t('Serial field value (unique per node type)'),
|
||||
// 'needs-data' => 'node',
|
||||
// );
|
||||
// return array(
|
||||
// 'types' => array('node' => $type),
|
||||
// 'tokens' => array('node' => $node),
|
||||
// );
|
||||
//}
|
||||
//
|
||||
///**
|
||||
// * Implements hook_tokens().
|
||||
// */
|
||||
//function serial_tokens($type, $tokens, $data, $options) {
|
||||
// // TODO
|
||||
//}
|
||||
|
||||
//=================//
|
||||
// Field Formatter //
|
||||
//=================//
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_info().
|
||||
*/
|
||||
@@ -157,7 +118,7 @@ function serial_field_formatter_info() {
|
||||
'serial_formatter_default' => array(
|
||||
'label' => t('Default'),
|
||||
'field types' => array('serial'),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -172,21 +133,17 @@ function serial_field_formatter_view($entity_type, $entity, $field, $instance, $
|
||||
$element[$delta] = array(
|
||||
'#markup' => theme('serial_formatter_default', array(
|
||||
'serial_id' => $item['value'],
|
||||
))
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Theme Functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function serial_theme() {
|
||||
|
||||
// Register the theme for the default formatter.
|
||||
return array(
|
||||
'serial_formatter_default' => array(
|
||||
@@ -204,10 +161,6 @@ function theme_serial_formatter_default($variables) {
|
||||
return $variables['serial_id'];
|
||||
}
|
||||
|
||||
//==============//
|
||||
// Field Widget //
|
||||
//==============//
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_info().
|
||||
*/
|
||||
@@ -228,7 +181,6 @@ function serial_field_widget(&$form, &$form_state, $field, $instance, $items, $d
|
||||
'value' => array(
|
||||
'#type' => 'hidden',
|
||||
'#default_value' => $items[$delta]['value'],
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user