diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 976589e..d9c8375 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -300,7 +300,7 @@ public function retrieveForm($form_id, &$form_state) { // fully bootstrapped (i.e. during installation) in which case // menu_get_item() is not available. if (!isset($form_state['build_info']['files']['menu']) && !defined('MAINTENANCE_MODE')) { - $item = menu_get_item(); + $item = $this->menuGetItem(); if (!empty($item['include_file'])) { // Do not use form_load_include() here, as the file is already loaded. // Anyway, self::getCache() is able to handle filepaths too. @@ -359,10 +359,10 @@ public function retrieveForm($form_id, &$form_state) { // Assign a default CSS class name based on $form_id. // This happens here and not in self::prepareForm() in order to allow the // form constructor function to override or remove the default class. - $form['#attributes']['class'][] = drupal_html_class($form_id); + $form['#attributes']['class'][] = $this->drupalHtmlClass($form_id); // Same for the base form ID, if any. if (isset($form_state['build_info']['base_form_id'])) { - $form['#attributes']['class'][] = drupal_html_class($form_state['build_info']['base_form_id']); + $form['#attributes']['class'][] = $this->drupalHtmlClass($form_state['build_info']['base_form_id']); } // We need to pass $form_state by reference in order for forms to modify it, @@ -464,7 +464,7 @@ public function prepareForm($form_id, &$form, &$form_state) { else { $form['#token'] = $form_id; $form['form_token'] = array( - '#id' => drupal_html_id('edit-' . $form_id . '-form-token'), + '#id' => $this->drupalHtmlId('edit-' . $form_id . '-form-token'), '#type' => 'token', '#default_value' => $this->csrfToken->get($form['#token']), // Form processing and validation requires this value, so ensure the @@ -479,7 +479,7 @@ public function prepareForm($form_id, &$form, &$form_state) { $form['form_id'] = array( '#type' => 'hidden', '#value' => $form_id, - '#id' => drupal_html_id("edit-$form_id"), + '#id' => $this->drupalHtmlId("edit-$form_id"), // Form processing and validation requires this value, so ensure the // submitted form value appears literally, regardless of custom #tree // and #parents being set elsewhere. @@ -487,7 +487,7 @@ public function prepareForm($form_id, &$form, &$form_state) { ); } if (!isset($form['#id'])) { - $form['#id'] = drupal_html_id($form_id); + $form['#id'] = $this->drupalHtmlId($form_id); } $form += $this->getElementInfo('form'); @@ -585,7 +585,7 @@ public function processForm($form_id, &$form, &$form_state) { // browser don't increment all the element IDs needlessly. if (!$this->getErrors()) { // In case of errors, do not break HTML IDs of other forms. - drupal_static_reset('drupal_html_id'); + $this->drupalStaticReset('drupal_html_id'); } if ($form_state['submitted'] && !$this->getErrors() && !$form_state['rebuild']) { @@ -596,7 +596,7 @@ public function processForm($form_id, &$form, &$form_state) { // possibly ending execution. We make sure we do not react to the batch // that is already being processed (if a batch operation performs a // self::submitForm). - if ($batch =& batch_get() && !isset($batch['current_set'])) { + if ($batch =& $this->batchGet() && !isset($batch['current_set'])) { // Store $form_state information in the batch definition. // We need the full $form_state when either: // - Some submit handlers were saved to be called during batch @@ -714,7 +714,7 @@ public function redirectForm($form_state) { // This function can be called from the installer, which guarantees // that $redirect will always be a string, so catch that case here // and use the appropriate redirect function. - if (drupal_installation_attempted()) { + if ($this->drupalInstallationAttempted()) { install_goto($form_state['redirect']); } else { @@ -825,7 +825,7 @@ public function doBuildForm($form_id, &$element, &$form_state) { } if (!isset($element['#id'])) { - $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents'])); + $element['#id'] = $this->drupalHtmlId('edit-' . implode('-', $element['#parents'])); } // Add the aria-describedby attribute to associate the form control with its @@ -851,7 +851,7 @@ public function doBuildForm($form_id, &$element, &$form_state) { // Recurse through all child elements. $count = 0; - foreach (element_children($element) as $key) { + foreach ($this->elementChildren($element) as $key) { // Prior to checking properties of child elements, their default properties // need to be loaded. if (isset($element[$key]['#type']) && empty($element[$key]['#defaults_loaded']) && ($info = $this->getElementInfo($element[$key]['#type']))) { @@ -1055,7 +1055,7 @@ public function validateForm($form_id, &$form, &$form_state) { */ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { // Recurse through all children. - foreach (element_children($elements) as $key) { + foreach ($this->elementChildren($elements) as $key) { if (isset($elements[$key]) && $elements[$key]) { $this->doValidateForm($elements[$key], $form_state); } @@ -1082,7 +1082,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { foreach ($value as $v) { if (!isset($options[$v])) { $this->setError($elements, t('An illegal choice has been detected. Please contact the site administrator.')); - watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); + $this->watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); } } } @@ -1101,7 +1101,7 @@ protected function doValidateForm(&$elements, &$form_state, $form_id = NULL) { } elseif (!isset($options[$elements['#value']])) { $this->setError($elements, t('An illegal choice has been detected. Please contact the site administrator.')); - watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); + $this->watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); } } } @@ -1253,7 +1253,7 @@ public function executeHandlers($type, &$form, &$form_state) { // Check if a previous _submit handler has set a batch, but make sure we // do not react to a batch that is already being processed (for instance // if a batch operation performs a self::submitForm()). - if ($type == 'submit' && ($batch =& batch_get()) && !isset($batch['id'])) { + if ($type == 'submit' && ($batch =& $this->batchGet()) && !isset($batch['id'])) { // Some previous submit handler has set a batch. To ensure correct // execution order, store the call in a special 'control' batch set. // See _batch_next_set(). @@ -1565,7 +1565,7 @@ public function setErrorByName($name = NULL, $message = '', $limit_validation_er if ($record) { $this->errors[$name] = $message; if ($message) { - drupal_set_message($message, 'error'); + $this->drupalSetMessage($message, 'error'); } } } @@ -1634,21 +1634,92 @@ protected function buttonWasClicked($element, &$form_state) { } /** - * Retrieves the default properties for the defined element type. - * - * @param string $type - * An element type as defined by hook_element_info(). + * Wraps element_info(). * * @return array - * The default properties for the defined element type. - * - * @todo Convert element_info() to service. */ protected function getElementInfo($type) { return element_info($type); } /** + * Wraps drupal_installation_attempted(). + * + * @return bool + */ + protected function drupalInstallationAttempted() { + return drupal_installation_attempted(); + } + + /** + * Wraps batch_get(). + * + * @return array() + */ + protected function &batchGet() { + return batch_get(); + } + + /** + * Wraps menu_get_item(). + * + * @return array|bool + */ + protected function menuGetItem() { + return menu_get_item(); + } + + /** + * Wraps watchdog(). + */ + protected function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { + watchdog($type, $message, $variables, $severity, $link); + } + + /** + * Wraps drupal_set_message(). + * + * @return array|null + */ + protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) { + return drupal_set_message($message, $type, $repeat); + } + + /** + * Wraps element_children(). + * + * @return array + */ + protected function elementChildren(&$elements, $sort = FALSE) { + return element_children($elements, $sort); + } + + /** + * Wraps drupal_html_class(). + * + * @return string + */ + protected function drupalHtmlClass($class) { + return drupal_html_class($class); + } + + /** + * Wraps drupal_html_id(). + * + * @return string + */ + protected function drupalHtmlId($id) { + return drupal_html_id($id); + } + + /** + * Wraps drupal_static_reset(). + */ + protected function drupalStaticReset($name = NULL) { + drupal_static_reset($name); + } + + /** * Gets the current active user. * * @return \Drupal\Core\Session\AccountInterface diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index 54d3da7..6e13dfe 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -426,6 +426,74 @@ protected function getElementInfo($type) { return $types[$type]; } + /** + * {@inheritdoc} + */ + protected function batchGet() { + return array(); + } + + /** + * {@inheritdoc} + */ + protected function drupalInstallationAttempted() { + return FALSE; + } + + /** + * {@inheritdoc} + */ + protected function menuGetItem() { + return FALSE; + } + + /** + * {@inheritdoc} + */ + protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) { + } + + /** + * {@inheritdoc} + */ + protected function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { + } + + /** + * {@inheritdoc} + */ + protected function elementChildren(&$elements, $sort = FALSE) { + $children = array(); + foreach ($elements as $key => $value) { + if ($key === '' || $key[0] !== '#') { + if (is_array($value)) { + $children[] = $key; + } + } + } + return $children; + } + + /** + * {@inheritdoc} + */ + protected function drupalHtmlClass($class) { + return $class; + } + + /** + * {@inheritdoc} + */ + protected function drupalHtmlId($id) { + return $id; + } + + /** + * {@inheritdoc} + */ + protected function drupalStaticReset($name = NULL) { + } + } } @@ -460,56 +528,4 @@ function test_form_id() { if (!defined('WATCHDOG_ERROR')) { define('WATCHDOG_ERROR', 3); } - - if (!function_exists('drupal_installation_attempted')) { - function drupal_installation_attempted() { - return FALSE; - } - } - if (!function_exists('file_create_url')) { - function file_create_url($uri) { - return $uri; - } - } - if (!function_exists('menu_get_item')) { - function menu_get_item() { - return FALSE; - } - } - if (!function_exists('watchdog')) { - function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG_NOTICE, $link = NULL) { - } - } - if (!function_exists('drupal_html_class')) { - function drupal_html_class($class) { - return $class; - } - } - if (!function_exists('drupal_html_id')) { - function drupal_html_id($id) { - return $id; - } - } - if (!function_exists('element_children')) { - function element_children(&$elements, $sort = FALSE) { - $children = array(); - foreach ($elements as $key => $value) { - if ($key === '' || $key[0] !== '#') { - if (is_array($value)) { - $children[] = $key; - } - } - } - return $children; - } - } - if (!function_exists('batch_get')) { - function batch_get() { - return array(); - } - } - if (!function_exists('drupal_static_reset')) { - function drupal_static_reset($name = TRUE) { - } - } }