updated etxlink, ctools, colorbox, computed_field

This commit is contained in:
2019-05-13 17:51:14 +02:00
parent 33210e10f2
commit 2ffad14939
309 changed files with 4930 additions and 2655 deletions

View File

@@ -3,11 +3,10 @@ description = Defines a field type that allows values to be "computed" via PHP c
core = 7.x
dependencies[] = field
package = Fields
files[]=computed_field.install
files[]=computed_field.module
; Information added by packaging script on 2013-12-03
version = "7.x-1.0"
; Information added by Drupal.org packaging script on 2016-01-21
version = "7.x-1.1"
core = "7.x"
project = "computed_field"
datestamp = "1386094705"
datestamp = "1453389259"

View File

@@ -1,21 +1,20 @@
<?php
/**
* @file
* Install, update and uninstall functions for the computed field module.
*/
/**
* Implements of hook_field_schema().
* Implements hook_field_schema().
*/
function computed_field_field_schema($field) {
if ($field['type'] == 'computed') {
$settings = $field['settings'];
if ($settings['store']) {
$columns = array('value' => array());
// Hardcoded 'longtext' settings
// Hardcoded 'longtext' settings.
if ($settings['database']['data_type'] == 'longtext') {
$columns['value']['type'] = 'text';
$columns['value']['size'] = 'big';
@@ -23,32 +22,32 @@ function computed_field_field_schema($field) {
else {
$columns['value']['type'] = isset($settings['database']['data_type']) ? $settings['database']['data_type'] : 'varchar';
}
// 'text' and 'varchar' fields get length settings
// 'text' and 'varchar' fields get length settings.
if ($settings['database']['data_type'] == 'text' || $settings['database']['data_type'] == 'varchar') {
$columns['value']['length'] = isset($settings['database']['data_length']) ? $settings['database']['data_length'] : 32;
}
// 'int' and 'float' fields get size settings
// 'int' and 'float' fields get size settings.
if ($settings['database']['data_type'] == 'int' || $settings['database']['data_type'] == 'float') {
$columns['value']['size'] = isset($settings['database']['data_size']) ? $settings['database']['data_size'] : 'normal';
}
// 'decimal' fields get precision and scale settings
// 'decimal' fields get precision and scale settings.
if ($settings['database']['data_type'] == 'numeric') {
$columns['value']['precision'] = isset($settings['database']['data_precision']) ? $settings['database']['data_precision'] : 10;
$columns['value']['scale'] = isset($settings['database']['data_scale']) ? $settings['database']['data_scale'] : 2;
}
// Add 'not null' settings
// Add 'not null' settings.
$columns['value']['not null'] = isset($settings['database']['data_not_NULL']) ? $settings['database']['data_not_NULL'] : TRUE;
// Add default values if set
if ($settings['database']['data_default'] != '') {
// Add default values if set.
if ($settings['database']['data_default'] !== '') {
$columns['value']['default'] = $settings['database']['data_default'];
}
// Add a simple index on the data if requested (except 'text' fields)
if ($settings['database']['data_index'] && $columns['value']['type'] != 'text') {
$indexes = array('value' => array('value'));
// Add a simple index on the data if requested (except 'text' fields).
if ($settings['database']['data_index'] && $columns['value']['type'] != 'text') {
$indexes = array('value' => array('value'));
}
}
}
if (isset($columns) && isset($indexes)) {
return array(
'columns' => $columns,
@@ -60,5 +59,5 @@ function computed_field_field_schema($field) {
'columns' => $columns,
);
}
else return;
return;
}

View File

@@ -1,7 +1,12 @@
<?php
/**
* Implements field hook_field_info().
* @file
* Functionality for the computed field.
*/
/**
* Implements hook_field_info().
*/
function computed_field_field_info() {
return array(
@@ -12,6 +17,7 @@ function computed_field_field_info() {
'code' => '$entity_field[0][\'value\'] = "";',
'display_format' => '$display_output = $entity_field_item[\'value\'];',
'store' => 1,
'recalculate' => FALSE,
'database' => array(
'data_type' => 'varchar',
'data_length' => 32,
@@ -40,12 +46,14 @@ function computed_field_field_info() {
function computed_field_entity_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
$property_types = array(
'int' => 'integer',
'float' => 'decimal', 'numeric' => 'decimal',
'varchar' => 'text', 'text' => 'text', 'longtext' => 'text',
'float' => 'decimal',
'numeric' => 'decimal',
'varchar' => 'text',
'text' => 'text',
'longtext' => 'text',
);
if (isset($field['columns']['value']) && isset($property_types[$field['columns']['value']['type']])) {
// Entity API's defaults are pretty good so set the property_type and let
// them do the work for us.
// Entity API's defaults are pretty good so set the property_type and let them do the work for us.
$field_type['property_type'] = $property_types[$field['columns']['value']['type']];
entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
// The only thing is that a setter doesn't make sense, so let's disable it.
@@ -55,7 +63,7 @@ function computed_field_entity_property_callback(&$info, $entity_type, $field, $
}
/**
* Implements of hook_field_settings_form().
* Implements hook_field_settings_form().
*/
function computed_field_field_settings_form($field, $instance, $has_data) {
$form = array();
@@ -69,14 +77,18 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
'#type' => 'textarea',
'#rows' => 15,
'#title' => t('Computed Code (PHP)'),
'#description' => t('The variables available to your code include: <code>@fields</code>. To set the value of the field, set <code>@entity_field</code>. For multi-value computed fields continue with <code>@entity_field_multi</code>. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (<code>@field_a</code> and <code>@field_b</code>) in a node entity:<p><code>@example</code> The first pop fetches the last (or only) item from the field while the second pop fetches its <code>[\'value\']</code> contents (assuming it\'s the only key that\'s set).<p>Alternately, this code can be supplied by your own custom function named: <code>@compute_func(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)</code>',
array('@fields' => '&$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items',
'@entity_field' => '$entity_field[0][\'value\']',
'@entity_field_multi' => '$entity_field[1][\'value\']',
'@field_a' => 'field_a',
'@field_b' => 'field_b',
'@example' => '$entity_field[0][\'value\'] = array_pop(array_pop(field_get_items($entity_type, $entity, \'field_a\'))) + array_pop(array_pop(field_get_items($entity_type, $entity, \'field_b\')));',
'@compute_func' => $compute_func)),
'#description' => t('<p>The variables available to your code include: <code>@fields</code>. To set the value of the field, set <code>@entity_field</code>. For multi-value computed fields continue with <code>@entity_field_multi</code>. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (<code>@field_a</code> and <code>@field_b</code>) in a node entity:</p> !example <p>Alternately, this code can be supplied by your own custom function named: <code>@compute_func(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)</code>.</p>', array(
'@fields' => '&$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items',
'@entity_field' => '$entity_field[0][\'value\']',
'@entity_field_multi' => '$entity_field[1][\'value\']',
'@field_a' => 'field_a',
'@field_b' => 'field_b',
'!example' => '<p><code>$field_a = field_get_items($entity_type, $entity, "field_a");<br />
$field_b = field_get_items($entity_type, $entity, "field_b");<br />
$entity_field[0]["value"] = $field_a[0]["value"] + $field_b[0]["value"];</code></p>
',
'@compute_func' => $compute_func,
)),
'#default_value' => !empty($settings['code']) ? $settings['code'] : '$entity_field[0][\'value\'] = "";',
'#access' => !function_exists($compute_func),
);
@@ -89,10 +101,11 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
$form['display_format'] = array(
'#type' => 'textarea',
'#title' => t('Display Code (PHP)'),
'#description' => t('This code should assign a string to the <code>@display_output</code> variable, which will be printed when the field is displayed. The raw computed value of the field is in <code>@value</code>. <strong>Note:</strong> this code has no effect if you use the "Raw computed value" display formatter.<p> Alternately, this code can be supplied by your own custom function named: <code>@display_func($field, $entity_field_item, $entity_lang, $langcode, $entity)</code>. Return the value to be displayed. Original value is in $entity_field_item[\'value\'].',
array('@display_output' => '$display_output',
'@value' => '$entity_field_item[\'value\']',
'@display_func' => $display_func)),
'#description' => t('This code should assign a string to the <code>@display_output</code> variable, which will be printed when the field is displayed. The raw computed value of the field is in <code>@value</code>. <strong>Note:</strong> this code has no effect if you use the "Raw computed value" display formatter.<p> Alternately, this code can be supplied by your own custom function named: <code>@display_func($field, $entity_field_item, $entity_lang, $langcode, $entity)</code>. Return the value to be displayed. Original value is in $entity_field_item[\'value\'].', array(
'@display_output' => '$display_output',
'@value' => '$entity_field_item[\'value\']',
'@display_func' => $display_func,
)),
'#default_value' => !empty($settings['display_format']) ? $settings['display_format'] : '$display_output = $entity_field_item[\'value\'];',
'#access' => !function_exists($display_func),
);
@@ -100,8 +113,14 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
$form['display_func'] = array(
'#type' => 'item',
'#markup' => t('<strong>This field is DISPLAYED using <code>@display_func()</code>.</strong>', array('@display_func' => $display_func)),
);
);
}
$form['recalculate'] = array(
'#type' => 'checkbox',
'#title' => t('Recalculate the field value every time.'),
'#description' => t('By default, Drupal will cache the value of this field even if it is not stored in the database (and even if Page Caching is disabled). This option will cause computed_field to recalculate the value every time this field is displayed. For example, a time-based calculated value may change more often than field cache is cleared. (Note that Drupal page caching will still cache the field value.)'),
'#default_value' => is_numeric($settings['recalculate']) ? $settings['recalculate'] : FALSE,
);
$form['store'] = array(
'#type' => 'checkbox',
'#title' => t('Store value in the database'),
@@ -122,7 +141,14 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
'#title' => t('Data Type'),
'#description' => t('The SQL datatype to store this field in.'),
'#default_value' => !empty($settings['database']['data_type']) ? $settings['database']['data_type'] : 'varchar',
'#options' => array('varchar' => 'varchar', 'text' => 'text', 'longtext' => 'longtext', 'int' => 'int', 'float' => 'float', 'numeric' => 'decimal'),
'#options' => array(
'varchar' => 'varchar',
'text' => 'text',
'longtext' => 'longtext',
'int' => 'int',
'float' => 'float',
'numeric' => 'decimal',
),
'#required' => FALSE,
'#disabled' => $has_data,
);
@@ -139,7 +165,13 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
'#title' => t('Data Size (int/float)'),
'#description' => t('<strong>Only</strong> valid for <strong>int</strong> or <strong>float</strong> fields. The size of the field stored in the database.'),
'#default_value' => !empty($settings['database']['data_size']) ? $settings['database']['data_size'] : 'normal',
'#options' => array('tiny' => 'tiny', 'small' => 'small', 'medium' => 'medium', 'normal' => 'normal', 'big' => 'big'),
'#options' => array(
'tiny' => 'tiny',
'small' => 'small',
'medium' => 'medium',
'normal' => 'normal',
'big' => 'big',
),
'#required' => FALSE,
'#disabled' => $has_data,
);
@@ -184,8 +216,8 @@ function computed_field_field_settings_form($field, $instance, $has_data) {
}
/**
* Implements the #element_validate callback for computed_field_field_settings_form().
*/
* #element_validate callback for computed_field_field_settings_form().
*/
function computed_field_field_settings_form_validate($element, &$form_state) {
$settings = $form_state['values']['field']['settings'];
if ($settings['store']) {
@@ -202,11 +234,11 @@ function computed_field_field_settings_form_validate($element, &$form_state) {
}
/**
* Implements field hook_field_load().
* Implements hook_field_load().
*/
function computed_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
$settings = $field['settings'];
// Compute field values on load if they aren't stored in the database
// Compute field values on load if they aren't stored in the database.
if (!$settings['store']) {
foreach ($entities as $etid => $entity) {
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
@@ -215,33 +247,34 @@ function computed_field_field_load($entity_type, $entities, $field, $instances,
}
/**
* Implements field hook_field_prepare_view().
* Implements hook_field_prepare_view().
*/
function computed_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
// Compute field values in case user is "previewing" an entity
$settings = $field['settings'];
// Compute field values in case user is "previewing" an entity.
foreach ($entities as $etid => $entity) {
if (isset($entity->op) && $entity->op == 'Preview') {
if ((isset($entity->op) && $entity->op == 'Preview') || $settings['recalculate']) {
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
}
}
}
/**
* Implements field hook_field_insert().
* Implements hook_field_insert().
*/
function computed_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements field hook_field_update().
* Implements hook_field_update().
*/
function computed_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements field hook_field_widget_info().
* Implements hook_field_widget_info().
*/
function computed_field_field_widget_info() {
return array(
@@ -257,14 +290,15 @@ function computed_field_field_widget_info() {
}
/**
* Implements field hook_field_widget_form().
* Implements hook_field_widget_form().
*/
function computed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// If there are no items yet, add a null item value to avoid
// preview errors when selecting a different language
if (empty($items)) $items[0]['value'] = NULL;
// preview errors when selecting a different language.
if (empty($items)) {
$items[0]['value'] = NULL;
}
foreach ($items as $item_delta => $item) {
$element[$item_delta]['value'] = array(
@@ -305,37 +339,45 @@ function computed_field_field_formatter_info() {
*/
function computed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$data_to_display = FALSE;
// Special case formatter that returns the raw computed values without any display code processing
// Special case formatter that returns the raw computed values without any display code processing.
if ($display['type'] == "computed_field_computed_value") {
foreach ($items as $delta => $item) {
if (!isset($entity_field_item['value'])) $entity_field_item['value'] = NULL;
if (!isset($entity_field_item['value'])) {
$entity_field_item['value'] = NULL;
}
$element[$delta] = array('#markup' => $item['value']);
}
return $element;
}
// Other display formatters which run through display code processing
// Check if the value is to be formatted by a display function outside the DB
// Other display formatters which run through display code processing.
// Check if the value is to be formatted by a display function outside the DB.
$display_func = 'computed_field_' . $field['field_name'] . '_display';
if (function_exists($display_func)) $display_in_code = TRUE;
else $display_in_code = FALSE;
$display_in_code = function_exists($display_func) ? TRUE : FALSE;
// Loop the items to display
// Loop the items to display.
foreach ($items as $delta => $item) {
// For "some" backwards compatibility
// For "some" backwards compatibility.
$entity_field_item = $item;
// Setup a variable with the entity language if available
if (isset($entity->language)) $entity_lang = $entity->language;
else $entity_lang = LANGUAGE_NONE;
// Setup a variable with the entity language if available.
if (isset($entity->language)) {
$entity_lang = $entity->language;
}
else {
$entity_lang = LANGUAGE_NONE;
}
// If there are value "holes" in the field array let's set the value to NULL
// to avoid undefined index errors in typical PHP display code
if (!isset($entity_field_item['value'])) $entity_field_item['value'] = NULL;
// If there are value "holes" in the field array let's set the value to NULL.
// to avoid undefined index errors in typical PHP display code.
if (!isset($entity_field_item['value'])) {
$entity_field_item['value'] = NULL;
}
// Execute the display code
// Execute the display code.
$display_output = NULL;
if ($display_in_code) {
$display_output = $display_func($field, $entity_field_item, $entity_lang, $langcode, $entity);
@@ -344,31 +386,59 @@ function computed_field_field_formatter_view($entity_type, $entity, $field, $ins
eval($field['settings']['display_format']);
}
// Output the formatted display item
// Track if any of our items produce non-empty output.
if (!empty($display_output) || is_numeric($display_output)) {
$data_to_display = TRUE;
}
// Output the formatted display item.
switch ($display['type']) {
case 'computed_field_unsanitized':
$element[$delta] = array('#markup' => $display_output);
break;
case 'computed_field_plain':
$element[$delta] = array('#markup' => check_plain($display_output));
break;
case 'computed_field_markup':
$element[$delta] = array('#markup' => check_markup($display_output));
break;
}
}
// If all items are empty then we should not return anything. This helps
// ensure that empty fields are not displayed at all. This check does not
// apply to fields stored in the DB as those are instead checked on save.
if (isset($field['settings']['store']) && !$field['settings']['store'] && !$data_to_display) {
return;
}
return $element;
}
/**
* Implements field hook_field_is_empty().
* Implements hook_field_is_empty().
*/
function computed_field_field_is_empty($item, $field) {
$data_type = $field['settings']['database']['data_type'];
if ($data_type == 'int' || $data_type == 'float') {
return !is_numeric($item['value']);
unset($empty);
// This will depend on the class of data type.
switch ($field['settings']['database']['data_type']) {
case 'int':
case 'float':
case 'numeric':
// For numbers, the field is empty if the value isn't numeric.
$empty = !is_numeric($item['value']);
break;
case 'varchar':
case 'text':
case 'longtext':
// For strings, the field is empty if it doesn't match the empty string.
$empty = ($item['value'] === "");
break;
}
return empty($item['value']);
return $empty;
}
/**
@@ -377,14 +447,18 @@ function computed_field_field_is_empty($item, $field) {
function _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, &$items) {
$settings = $field['settings'];
// Setup a variable with the field values
// Setup a variable with the field values.
$entity_field =& $items;
// Setup a variable with the entity language if available
if (isset($entity->language)) $entity_lang = $entity->language;
else $entity_lang = LANGUAGE_NONE;
// Setup a variable with the entity language if available.
if (isset($entity->language)) {
$entity_lang = $entity->language;
}
else {
$entity_lang = LANGUAGE_NONE;
}
// Allow the value to be computed from code not stored in DB
// Allow the value to be computed from code not stored in DB.
$compute_func = 'computed_field_' . $field['field_name'] . '_compute';
if (function_exists($compute_func)) {
$compute_func($entity_field, $entity_type, $entity, $field, $instance, $langcode, $items);
@@ -395,4 +469,3 @@ function _computed_field_compute_value($entity_type, $entity, $field, $instance,
}
}
}