Newer
Older
/**
* @file
* Common functions that many Drupal modules will need to reference.
*
* The functions that are critical and need to be available even when serving
* a cached page are instead located in bootstrap.inc.
*/
Angie Byron
committed
use Drupal\Component\Serialization\Json;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Utility\Bytes;
Angie Byron
committed
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
catch
committed
use Drupal\Component\Utility\Number;
use Drupal\Component\Utility\SortArray;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Tags;
Dries Buytaert
committed
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Renderer;
Alex Pott
committed
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Routing\GeneratorNotInitializedException;
Dries Buytaert
committed
use Drupal\Core\Template\Attribute;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AnonymousUserSession;
Larry Garfield
committed
Dries Buytaert
committed
/**
* @defgroup php_wrappers PHP wrapper functions
* @{
* Functions that are wrappers or custom implementations of PHP functions.
*
* Certain PHP functions should not be used in Drupal. Instead, Drupal's
* replacement functions should be used.
*
* For example, for improved or more secure UTF8-handling, or RFC-compliant
* handling of URLs in Drupal.
*
* For ease of use and memorizing, all these wrapper functions use the same name
* as the original PHP function, but prefixed with "drupal_". Beware, however,
* that not all wrapper functions support the same arguments as the original
* functions.
*
* You should always use these wrapper functions in your code.
*
* Wrong:
* @code
* $my_substring = substr($original_string, 0, 5);
* @endcode
*
* Correct:
* @code
* $my_substring = Unicode::substr($original_string, 0, 5);
Dries Buytaert
committed
* @endcode
*
* @}
Dries Buytaert
committed
*/
Steven Wittens
committed
/**
* Return status for saving which involved creating a new item.
*/
const SAVED_NEW = 1;
Steven Wittens
committed
/**
* Return status for saving which involved an update to an existing item.
*/
const SAVED_UPDATED = 2;
Steven Wittens
committed
/**
* Return status for saving which deleted an existing item.
*/
const SAVED_DELETED = 3;
Steven Wittens
committed
Dries Buytaert
committed
/**
Angie Byron
committed
* The default aggregation group for CSS files added to the page.
Dries Buytaert
committed
*/
Angie Byron
committed
const CSS_AGGREGATE_DEFAULT = 0;
Dries Buytaert
committed
/**
Angie Byron
committed
* The default aggregation group for theme CSS files added to the page.
Dries Buytaert
committed
*/
Angie Byron
committed
const CSS_AGGREGATE_THEME = 100;
/**
* The default weight for CSS rules that style HTML elements ("base" styles).
*/
const CSS_BASE = -200;
/**
* The default weight for CSS rules that layout a page.
*/
const CSS_LAYOUT = -100;
/**
Alex Pott
committed
* The default weight for CSS rules that style design components (and their associated states and themes.)
Angie Byron
committed
*/
const CSS_COMPONENT = 0;
/**
* The default weight for CSS rules that style states and are not included with components.
*/
const CSS_STATE = 100;
/**
Alex Pott
committed
* The default weight for CSS rules that style themes and are not included with components.
Angie Byron
committed
*/
Alex Pott
committed
const CSS_THEME = 200;
Dries Buytaert
committed
/**
* The default group for JavaScript settings added to the page.
*/
const JS_SETTING = -200;
Angie Byron
committed
/**
catch
committed
* The default group for JavaScript and jQuery libraries added to the page.
Angie Byron
committed
*/
const JS_LIBRARY = -100;
Angie Byron
committed
/**
Dries Buytaert
committed
* The default group for module JavaScript code added to the page.
Angie Byron
committed
*/
const JS_DEFAULT = 0;
Angie Byron
committed
/**
Dries Buytaert
committed
* The default group for theme JavaScript code added to the page.
Angie Byron
committed
*/
const JS_THEME = 100;
Angie Byron
committed
Dries Buytaert
committed
/**
* The delimiter used to split plural strings.
*
* This is the ETX (End of text) character and is used as a minimal means to
* separate singular and plural variants in source and translation text. It
* was found to be the most compatible delimiter for the supported databases.
*/
const LOCALE_PLURAL_DELIMITER = "\03";
catch
committed
* Adds output to the HEAD tag of the HTML page.
* This function can be called as long as the headers aren't sent. Pass no
* arguments (or NULL for both) to retrieve the currently stored elements.
*
* @param $data
* A renderable array. If the '#type' key is not set then 'html_tag' will be
* added as the default '#type'.
* @param $key
* A unique string key to allow implementations of hook_html_head_alter() to
* identify the element in $data. Required if $data is not NULL.
*
* @return
* An array of all stored HEAD elements.
*
Jennifer Hodgdon
committed
* @see \Drupal\Core\Render\Element\HtmlTag::preRenderHtmlTag()
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use #attached on render arrays.
function _drupal_add_html_head($data = NULL, $key = NULL) {
catch
committed
$stored_head = &drupal_static(__FUNCTION__, array());
if (isset($data) && isset($key)) {
if (!isset($data['#type'])) {
$data['#type'] = 'html_tag';
}
$stored_head[$key] = $data;
/**
catch
committed
* Retrieves output to be displayed in the HEAD tag of the HTML page.
catch
committed
*
* @param bool $render
* If TRUE render the HEAD elements, otherwise return just the elements.
*
* @return string|array
* Return the rendered HTML head or the elements itself.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use #attached on render arrays.
Dries Buytaert
committed
*/
catch
committed
function drupal_get_html_head($render = TRUE) {
$elements = _drupal_add_html_head();
\Drupal::moduleHandler()->alter('html_head', $elements);
catch
committed
if ($render) {
return drupal_render($elements);
}
else {
return $elements;
}
/**
* Prepares a 'destination' URL query parameter for use with url().
* Used to direct the user back to the referring page after completing a form.
* By default the current URL is returned. If a destination exists in the
* previous request, that destination is returned. As such, a destination can
* persist across multiple pages.
*
* @return array
Jennifer Hodgdon
committed
* An associative array containing the key:
* - destination: The value of the current request's 'destination' query
* parameter, if present. This can be either a relative or absolute URL.
* However, for security, redirection to external URLs is not performed.
* If the query parameter isn't present, then the URL of the current
* request is returned.
Jennifer Hodgdon
committed
*
* @see \Drupal\Core\EventSubscriber\RedirectResponseSubscriber::checkRedirectUrl()
* @ingroup form_api
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use the redirect.destination service.
*/
function drupal_get_destination() {
return \Drupal::destination()->getAsArray();
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
* Verifies the syntax of the given email address.
* @param string $mail
* A string containing an email address.
catch
committed
*
* @return bool
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal::service('email.validator')->isValid().
Dries Buytaert
committed
*/
return \Drupal::service('email.validator')->isValid($mail);
Dries Buytaert
committed
}
/**
* @} End of "defgroup validation".
*/
/**
* @defgroup sanitization Sanitization functions
* @{
* Functions to sanitize values.
Dries Buytaert
committed
*
* See http://drupal.org/writing-secure-code for information
* on writing secure code.
/**
catch
committed
* Strips dangerous protocols from a URI and encodes it for output to HTML.
*
* @param $uri
* A plain-text URI that might contain dangerous protocols.
*
* @return
* A URI stripped of dangerous protocols and encoded for output to an HTML
* attribute value. Because it is already encoded, it should not be set as a
* value within a $attributes array passed to Drupal\Core\Template\Attribute,
* because Drupal\Core\Template\Attribute expects those values to be
* plain-text strings. To pass a filtered URI to
Angie Byron
committed
* Drupal\Core\Template\Attribute, call
Dries Buytaert
committed
* \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() instead.
*
Dries Buytaert
committed
* @see \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols()
* @see \Drupal\Component\Utility\SafeMarkup::checkPlain()
return SafeMarkup::checkPlain(UrlHelper::stripDangerousProtocols($uri));
/**
* @} End of "defgroup sanitization".
*/
/**
catch
committed
* Formats XML elements.
*
* Note: It is the caller's responsibility to sanitize any input parameters.
* This function does not perform sanitization.
*
* @param $array
* An array where each item represents an element and is either a:
* - (key => value) pair (<key>value</key>)
* - Associative array with fields:
* - 'key': The element name. Element names are not sanitized, so do not
* pass user input.
* - 'value': element contents
* - 'attributes': associative array of element attributes
*
* In both cases, 'value' can be a simple string, or it can be another array
* with the same format as $array itself for nesting.
*/
function format_xml_elements($array) {
Steven Wittens
committed
$output = '';
foreach ($array as $key => $value) {
if (is_numeric($key)) {
$output .= ' <' . $value['key'];
Dries Buytaert
committed
if (isset($value['attributes']) && is_array($value['attributes'])) {
$output .= new Attribute($value['attributes']);
if (isset($value['value']) && $value['value'] != '') {
$output .= '>' . (is_array($value['value']) ? format_xml_elements($value['value']) : SafeMarkup::checkPlain($value['value'])) . '</' . $value['key'] . ">\n";
$output .= ' <' . $key . '>' . (is_array($value) ? format_xml_elements($value) : SafeMarkup::checkPlain($value)) . "</$key>\n";
// @todo This is marking the output string as safe HTML, but we have only
// sanitized the attributes and tag values, not the tag names, and we
// cannot guarantee the assembled markup is safe. Consider a fix in:
// https://www.drupal.org/node/2296885
return SafeMarkup::set($output);
catch
committed
* Generates a string representation for the given byte count.
* A size in bytes.
Gábor Hojtsy
committed
* @param $langcode
* Optional language code to translate to a language other than what is used
* to display the page.
catch
committed
*
Gábor Hojtsy
committed
function format_size($size, $langcode = NULL) {
if ($size < Bytes::KILOBYTE) {
return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode));
$size = $size / Bytes::KILOBYTE; // Convert bytes to kilobytes.
Dries Buytaert
committed
$units = array(
Dries Buytaert
committed
t('@size KB', array(), array('langcode' => $langcode)),
t('@size MB', array(), array('langcode' => $langcode)),
t('@size GB', array(), array('langcode' => $langcode)),
t('@size TB', array(), array('langcode' => $langcode)),
t('@size PB', array(), array('langcode' => $langcode)),
t('@size EB', array(), array('langcode' => $langcode)),
t('@size ZB', array(), array('langcode' => $langcode)),
t('@size YB', array(), array('langcode' => $langcode)),
Dries Buytaert
committed
);
foreach ($units as $unit) {
if (round($size, 2) >= Bytes::KILOBYTE) {
$size = $size / Bytes::KILOBYTE;
Dries Buytaert
committed
}
else {
break;
}
Dries Buytaert
committed
return str_replace('@size', round($size, 2), $unit);
* Formats a date, using a date type or a custom date format string.
* A UNIX timestamp to format.
* (optional) The format to use, one of:
Angie Byron
committed
* - One of the built-in formats: 'short', 'medium',
* 'long', 'html_datetime', 'html_date', 'html_time',
* 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
* - The name of a date type defined by a date format config entity.
* - The machine name of an administrator-defined date format.
* - 'custom', to use $format.
* Defaults to 'medium'.
* (optional) If $type is 'custom', a PHP date format string suitable for
* input to date(). Use a backslash to escape ordinary text, so it does not
* get interpreted as date format characters.
* (optional) Time zone identifier, as described at
* http://php.net/manual/timezones.php Defaults to the time zone used to
* display the page.
Gábor Hojtsy
committed
* @param $langcode
* (optional) Language code to translate to. Defaults to the language used to
* display the page.
*
*
* @see \Drupal\Core\Datetime\DateFormatter::format()
Gábor Hojtsy
committed
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
Alex Pott
committed
return \Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
* Returns an ISO8601 formatted date based on the given date.
*
* @param $date
* A UNIX timestamp.
catch
committed
*
Dries Buytaert
committed
* @return string
* An ISO8601 formatted date.
*/
function date_iso8601($date) {
// The DATE_ISO8601 constant cannot be used here because it does not match
// date('c') and produces invalid RDF markup.
return date('c', $date);
}
Dries Buytaert
committed
/**
catch
committed
* Translates a formatted date string.
*
* Callback for preg_replace_callback() within format_date().
Dries Buytaert
committed
*/
function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
// We cache translations to avoid redundant and rather costly calls to t().
static $cache, $langcode;
if (!isset($matches)) {
$langcode = $new_langcode;
return;
}
$code = $matches[1];
$string = $matches[2];
if (!isset($cache[$langcode][$code][$string])) {
$options = array(
'langcode' => $langcode,
);
if ($code == 'F') {
$options['context'] = 'Long month name';
Dries Buytaert
committed
if ($code == '') {
$cache[$langcode][$code][$string] = $string;
Dries Buytaert
committed
}
Dries Buytaert
committed
$cache[$langcode][$code][$string] = t($string, array(), $options);
Dries Buytaert
committed
return $cache[$langcode][$code][$string];
/**
catch
committed
* Formats an attribute string for an HTTP header.
*
* @param $attributes
* An associative array of attributes such as 'rel'.
*
* @return
* A ; separated string ready for insertion in a HTTP header. No escaping is
* performed for HTML entities, so this string is not safe to be printed.
*/
function drupal_http_header_attributes(array $attributes = array()) {
foreach ($attributes as $attribute => &$data) {
if (is_array($data)) {
$data = implode(' ', $data);
}
$data = $attribute . '="' . $data . '"';
}
return $attributes ? ' ' . implode('; ', $attributes) : '';
}
/**
* Attempts to set the PHP maximum execution time.
*
* This function is a wrapper around the PHP function set_time_limit().
* When called, set_time_limit() restarts the timeout counter from zero.
* In other words, if the timeout is the default 30 seconds, and 25 seconds
* into script execution a call such as set_time_limit(20) is made, the
* script will run for a total of 45 seconds before timing out.
Angie Byron
committed
*
Angie Byron
committed
* If the current time limit is not unlimited it is possible to decrease the
* total time limit if the sum of the new time limit and the current time spent
* running the script is inferior to the original time limit. It is inherent to
* the way set_time_limit() works, it should rather be called with an
* appropriate value every time you need to allocate a certain amount of time
* to execute a task than only once at the beginning of the script.
Angie Byron
committed
*
* Before calling set_time_limit(), we check if this function is available
* because it could be disabled by the server administrator. We also hide all
* the errors that could occur when calling set_time_limit(), because it is
* not possible to reliably ensure that PHP or a security extension will
* not issue a warning/error if they prevent the use of this function.
*
* @param $time_limit
* An integer specifying the new time limit, in seconds. A value of 0
* indicates unlimited execution time.
Dries Buytaert
committed
*
* @ingroup php_wrappers
*/
function drupal_set_time_limit($time_limit) {
if (function_exists('set_time_limit')) {
Angie Byron
committed
$current = ini_get('max_execution_time');
// Do not set time limit if it is currently unlimited.
if ($current != 0) {
Angie Byron
committed
@set_time_limit($time_limit);
}
}
}
catch
committed
* Returns the base URL path (i.e., directory) of the Drupal installation.
Dries Buytaert
committed
*
catch
committed
* base_path() adds a "/" to the beginning and end of the returned path if the
* path is not empty. At the very least, this will return "/".
Dries Buytaert
committed
*
Dries Buytaert
committed
* Examples:
* - http://example.com returns "/" because the path is empty.
* - http://example.com/drupal/folder returns "/drupal/folder/".
Larry Garfield
committed
return $GLOBALS['base_path'];
catch
committed
* Adds a LINK tag with a distinct 'rel' attribute to the page's HEAD.
*
catch
committed
* This function can be called as long the HTML header hasn't been sent, which
* on normal pages is up through the preprocess step of _theme('html'). Adding
catch
committed
* a link will overwrite a prior link with the exact same 'rel' and 'href'
* attributes.
Dries Buytaert
committed
*
* @param $attributes
* Associative array of element attributes including 'href' and 'rel'.
* @param $header
* Optional flag to determine if a HTTP 'Link:' header should be sent.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use #attached on render arrays.
*/
function _drupal_add_html_head_link($attributes, $header = FALSE) {
$element = array(
'#tag' => 'link',
'#attributes' => $attributes,
);
$href = $attributes['href'];
if ($header) {
// Also add a HTTP header "Link:".
$href = '<' . SafeMarkup::checkPlain($attributes['href']) . '>;';
unset($attributes['href']);
$element['#attached']['http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE);
}
_drupal_add_html_head($element, 'html_head_link:' . $attributes['rel'] . ':' . $href);
Steven Wittens
committed
/**
* Deletes old cached CSS files.
catch
committed
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\Core\Asset\AssetCollectionOptimizerInterface::deleteAll().
*/
function drupal_clear_css_cache() {
\Drupal::service('asset.css.collection_optimizer')->deleteAll();
}
* Constructs an array of the defaults that are used for JavaScript assets.
Angie Byron
committed
*
* @param $data
* (optional) The default data parameter for the JavaScript asset array.
catch
committed
*
* @see hook_js_alter()
Angie Byron
committed
*/
function drupal_js_defaults($data = NULL) {
return array(
'type' => 'file',
Dries Buytaert
committed
'group' => JS_DEFAULT,
'every_page' => FALSE,
'weight' => 0,
Angie Byron
committed
'scope' => 'header',
'cache' => TRUE,
Dries Buytaert
committed
'preprocess' => TRUE,
'attributes' => array(),
Dries Buytaert
committed
'version' => NULL,
Angie Byron
committed
'data' => $data,
'browsers' => array(),
Angie Byron
committed
);
}
/**
* Merges two #attached arrays.
*
* The values under the 'drupalSettings' key are merged in a special way, to
* match the behavior of:
*
* @code
* jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...)
* @endcode
*
* This means integer indices are preserved just like string indices are,
* rather than re-indexed as is common in PHP array merging.
*
* Example:
* @code
* function module1_page_attachments(&$page) {
* $page['a']['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
* }
* function module2_page_attachments(&$page) {
* $page['#attached']['drupalSettings']['foo'] = ['d'];
* }
* // When the page is rendered after the above code, and the browser runs the
* // resulting <SCRIPT> tags, the value of drupalSettings.foo is
* // ['d', 'b', 'c'], not ['a', 'b', 'c', 'd'].
* @endcode
*
* By following jQuery.extend() merge logic rather than common PHP array merge
* logic, the following are ensured:
* - Attaching JavaScript settings is idempotent: attaching the same settings
* twice does not change the output sent to the browser.
* - If pieces of the page are rendered in separate PHP requests and the
* returned settings are merged by JavaScript, the resulting settings are the
* same as if rendered in one PHP request and merged by PHP.
*
* @param array $a
* An #attached array.
* @param array $b
* Another #attached array.
*
* @return array
* The merged #attached array.
*
* @deprecated To be removed in Drupal 8.0.x. Use
* \Drupal\Core\Render\Renderer::mergeAttachments() instead.
*/
function drupal_merge_attached(array $a, array $b) {
Alex Pott
committed
return \Drupal::service('renderer')->mergeAttachments($a, $b);
}
/**
* Processes non-asset attachments in a render() structure.
* Libraries, JavaScript settings, feeds, HTML <head> tags and HTML <head> links
* are attached to elements using the #attached property. The #attached property
* is an associative array, where the keys are the attachment types and the
* values are the attached data. For example:
Angie Byron
committed
*
Dries Buytaert
committed
* @code
* $build['#attached'] = [
* 'library' => ['core/jquery']
* ];
* $build['#attached']['http_header'] = [
* ['Content-Type', 'application/rss+xml; charset=utf-8'],
* ];
Dries Buytaert
committed
* @endcode
*
* The available keys are:
* - 'library' (asset libraries)
* - 'drupalSettings' (JavaScript settings)
* - 'feed' (RSS feeds)
* - 'html_head' (tags in HTML <head>)
* - 'html_head_link' (<link> tags in HTML <head>)
* - 'http_header' (HTTP headers)
*
* This function processes all non-asset attachments, to apply them to the
* current response (that means all keys except 'library' and 'drupalSettings').
Dries Buytaert
committed
*
* @param array $elements
Dries Buytaert
committed
* The structured array describing the data being rendered.
*
Dries Buytaert
committed
* @see drupal_render()
* @see \Drupal\Core\Asset\AssetResolver
*
* @throws LogicException
* When attaching something of a non-existing attachment type.
*/
function drupal_process_attached(array $elements) {
// Asset attachments are handled by \Drupal\Core\Asset\AssetResolver.
foreach (array('library', 'drupalSettings') as $type) {
unset($elements['#attached'][$type]);
}
Dries Buytaert
committed
// Add additional types of attachments specified in the render() structure.
foreach ($elements['#attached'] as $callback => $options) {
Dries Buytaert
committed
foreach ($elements['#attached'][$callback] as $args) {
// Limit the amount allowed entries.
switch ($callback) {
case 'html_head':
call_user_func_array('_drupal_add_html_head', $args);
break;
case 'feed':
catch
committed
$args = [[
'href' => $args[0],
'rel' => 'alternate',
'title' => $args[1],
'type' => 'application/rss+xml',
]];
call_user_func_array('_drupal_add_html_head_link', $args);
break;
case 'html_head_link':
call_user_func_array('_drupal_add_html_head_link', $args);
break;
case 'http_header':
call_user_func_array('_drupal_add_http_header', $args);
break;
default:
throw new \LogicException(sprintf('You are not allowed to use %s in #attached', $callback));
}
Dries Buytaert
committed
}
}
}
Dries Buytaert
committed
/**
* Adds JavaScript to change the state of an element based on another element.
Dries Buytaert
committed
*
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
* A "state" means a certain property on a DOM element, such as "visible" or
* "checked". A state can be applied to an element, depending on the state of
* another element on the page. In general, states depend on HTML attributes and
* DOM element properties, which change due to user interaction.
*
* Since states are driven by JavaScript only, it is important to understand
* that all states are applied on presentation only, none of the states force
* any server-side logic, and that they will not be applied for site visitors
* without JavaScript support. All modules implementing states have to make
* sure that the intended logic also works without JavaScript being enabled.
*
* #states is an associative array in the form of:
* @code
* array(
* STATE1 => CONDITIONS_ARRAY1,
* STATE2 => CONDITIONS_ARRAY2,
* ...
* )
* @endcode
* Each key is the name of a state to apply to the element, such as 'visible'.
* Each value is a list of conditions that denote when the state should be
* applied.
*
* Multiple different states may be specified to act on complex conditions:
* @code
* array(
* 'visible' => CONDITIONS,
* 'checked' => OTHER_CONDITIONS,
* )
* @endcode
*
* Every condition is a key/value pair, whose key is a jQuery selector that
* denotes another element on the page, and whose value is an array of
* conditions, which must bet met on that element:
* @code
* array(
* 'visible' => array(
* JQUERY_SELECTOR => REMOTE_CONDITIONS,
* JQUERY_SELECTOR => REMOTE_CONDITIONS,
* ...
* ),
* )
* @endcode
* All conditions must be met for the state to be applied.
*
* Each remote condition is a key/value pair specifying conditions on the other
* element that need to be met to apply the state to the element:
* @code
* array(
* 'visible' => array(
* ':input[name="remote_checkbox"]' => array('checked' => TRUE),
* ),
* )
* @endcode
*
* For example, to show a textfield only when a checkbox is checked:
* @code
* $form['toggle_me'] = array(
* '#type' => 'checkbox',
* '#title' => t('Tick this box to type'),
* );
* $form['settings'] = array(
* '#type' => 'textfield',
* '#states' => array(
* // Only show this field when the 'toggle_me' checkbox is enabled.
* 'visible' => array(
* ':input[name="toggle_me"]' => array('checked' => TRUE),
Dries Buytaert
committed
* ),
* ),
* );
* @endcode
*
* The following states may be applied to an element:
* - enabled
* - disabled
Dries Buytaert
committed
* - required
* - optional
* - visible
* - invisible
* - checked
* - unchecked
* - expanded
* - collapsed
*
* The following states may be used in remote conditions:
Dries Buytaert
committed
* - empty
* - filled
* - checked
* - unchecked
Dries Buytaert
committed
* - expanded
* - collapsed
* - value
*
Dries Buytaert
committed
* The following states exist for both elements and remote conditions, but are
* not fully implemented and may not change anything on the element:
* - relevant
* - irrelevant
* - valid
* - invalid
* - touched
* - untouched
* - readwrite
* - readonly
*
* When referencing select lists and radio buttons in remote conditions, a
* 'value' condition must be used:
* @code
* '#states' => array(
* // Show the settings if 'bar' has been selected for 'foo'.
* 'visible' => array(
* ':input[name="foo"]' => array('value' => 'bar'),
* ),
* ),
* @endcode
*
* @param $elements
* A renderable array element having a #states property as described above.
*
* @see form_example_states_form()
Dries Buytaert
committed
*/
function drupal_process_states(&$elements) {
$elements['#attached']['library'][] = 'core/drupal.states';
Angie Byron
committed
// Elements of '#type' => 'item' are not actual form input elements, but we
// still want to be able to show/hide them. Since there's no actual HTML input
// element available, setting #attributes does not make sense, but a wrapper
// is available, so setting #wrapper_attributes makes it work.
$key = ($elements['#type'] == 'item') ? '#wrapper_attributes' : '#attributes';
$elements[$key]['data-drupal-states'] = JSON::encode($elements['#states']);
Dries Buytaert
committed
}
Dries Buytaert
committed
/**
* Assists in attaching the tableDrag JavaScript behavior to a themed table.
Dries Buytaert
committed
*
* Draggable tables should be used wherever an outline or list of sortable items
* needs to be arranged by an end-user. Draggable tables are very flexible and
* can manipulate the value of form elements placed within individual columns.
*
catch
committed
* To set up a table to use drag and drop in place of weight select-lists or in
* place of a form that contains parent relationships, the form must be themed
* into a table. The table must have an ID attribute set and it
* may be set as follows:
Dries Buytaert
committed
* @code
* $table = array(
* '#type' => 'table',
* '#header' => $header,
* '#rows' => $rows,
* '#attributes' => array(
* 'id' => 'my-module-table',
* ),
* );
* return drupal_render($table);
Dries Buytaert
committed
* @endcode
*
* In the theme function for the form, a special class must be added to each
* form element within the same column, "grouping" them together.
*
* In a situation where a single weight column is being sorted in the table, the
* classes could be added like this (in the theme function):
* @code
* $form['my_elements'][$delta]['weight']['#attributes']['class'] = array('my-elements-weight');
Dries Buytaert
committed
* @endcode
*
catch
committed
* Each row of the table must also have a class of "draggable" in order to
* enable the drag handles:
Gábor Hojtsy
committed
* @code
* $row = array(...);
* $rows[] = array(
* 'data' => $row,
* 'class' => array('draggable'),
Gábor Hojtsy
committed
* );
* @endcode
Gábor Hojtsy
committed
* When tree relationships are present, the two additional classes
* 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior:
* - Rows with the 'tabledrag-leaf' class cannot have child rows.
* - Rows with the 'tabledrag-root' class cannot be nested under a parent row.
*
* Calling drupal_attach_tabledrag() would then be written as such:
Dries Buytaert
committed
* @code
* drupal_attach_tabledrag('my-module-table', array(
* 'action' => 'order',
* 'relationship' => 'sibling',
* 'group' => 'my-elements-weight',
* );
Dries Buytaert
committed
* @endcode
*
* In a more complex case where there are several groups in one column (such as
catch
committed
* the block regions on the admin/structure/block page), a separate subgroup
* class must also be added to differentiate the groups.
Dries Buytaert
committed
* @code
* $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);
Dries Buytaert
committed
* @endcode
*
* The 'group' option is still 'my-element-weight', and the additional
* 'subgroup' option will be passed in as 'my-elements-weight-' . $region. This
* also means that you'll need to call drupal_attach_tabledrag() once for every
* region added.
Dries Buytaert
committed
*
* @code
* foreach ($regions as $region) {
* drupal_attach_tabledrag('my-module-table', array(
* 'action' => 'order',
* 'relationship' => sibling',
* 'group' => 'my-elements-weight',
* 'subgroup' => my-elements-weight-' . $region,
* ));
Dries Buytaert
committed
* }
* @endcode
*
* In a situation where tree relationships are present, adding multiple
* subgroups is not necessary, because the table will contain indentations that
catch
committed
* provide enough information about the sibling and parent relationships. See
* MenuForm::BuildOverviewForm for an example creating a table
* containing parent relationships.
catch
committed
*
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
* @param $element
* A form element to attach the tableDrag behavior to.
* @param array $options
* These options are used to generate JavaScript settings necessary to
* configure the tableDrag behavior appropriately for this particular table.
* An associative array containing the following keys:
* - 'table_id': String containing the target table's id attribute.
* If the table does not have an id, one will need to be set,
* such as <table id="my-module-table">.
* - 'action': String describing the action to be done on the form item.
* Either 'match' 'depth', or 'order':
* - 'match' is typically used for parent relationships.
* - 'order' is typically used to set weights on other form elements with
* the same group.
* - 'depth' updates the target element with the current indentation.
* - 'relationship': String describing where the "action" option
* should be performed. Either 'parent', 'sibling', 'group', or 'self':
* - 'parent' will only look for fields up the tree.
* - 'sibling' will look for fields in the same group in rows above and
* below it.
* - 'self' affects the dragged row itself.
* - 'group' affects the dragged row, plus any children below it (the entire
* dragged group).
* - 'group': A class name applied on all related form elements for this action.
* - 'subgroup': (optional) If the group has several subgroups within it, this
* string should contain the class name identifying fields in the same
* subgroup.
* - 'source': (optional) If the $action is 'match', this string should contain
* the classname identifying what field will be used as the source value
* when matching the value in $subgroup.
* - 'hidden': (optional) The column containing the field elements may be
* entirely hidden from view dynamically when the JavaScript is loaded. Set
* to FALSE if the column should not be hidden.
* - 'limit': (optional) Limit the maximum amount of parenting in this table.
*
* @see MenuForm::BuildOverviewForm()
Dries Buytaert
committed
*/
function drupal_attach_tabledrag(&$element, array $options) {
// Add default values to elements.
$options = $options + array(
'subgroup' => NULL,
'source' => NULL,
'hidden' => TRUE,
'limit' => 0
);
$group = $options['group'];