Commit db30eaf4 authored by webchick's avatar webchick

Issue #2328785 by tim.plunkett: Convert form_load_include() to FormState::loadInclude().

parent 469fe97e
......@@ -41,55 +41,6 @@ function form_set_cache($form_build_id, $form, FormStateInterface $form_state) {
\Drupal::formBuilder()->setCache($form_build_id, $form, $form_state);
}
/**
* Ensures an include file is loaded whenever the form is processed.
*
* Example:
* @code
* // Load node.admin.inc from Node module.
* form_load_include($form_state, 'inc', 'node', 'node.admin');
* @endcode
*
* Use this function instead of module_load_include() from inside a form
* constructor or any form processing logic as it ensures that the include file
* is loaded whenever the form is processed. In contrast to using
* module_load_include() directly, form_load_include() makes sure the include
* file is correctly loaded also if the form is cached.
*
* @param $form_state
* The current state of the form.
* @param $type
* The include file's type (file extension).
* @param $module
* The module to which the include file belongs.
* @param $name
* (optional) The base file name (without the $type extension). If omitted,
* $module is used; i.e., resulting in "$module.$type" by default.
*
* @return
* The filepath of the loaded include file, or FALSE if the include file was
* not found or has been loaded already.
*
* @see module_load_include()
*/
function form_load_include(FormStateInterface $form_state, $type, $module, $name = NULL) {
if (!isset($name)) {
$name = $module;
}
if (!isset($form_state['build_info']['files']["$module:$name.$type"])) {
// Only add successfully included files to the form state.
if ($result = module_load_include($type, $module, $name)) {
$form_state['build_info']['files']["$module:$name.$type"] = array(
'type' => $type,
'module' => $module,
'name' => $name,
);
return $result;
}
}
return FALSE;
}
/**
* Retrieves, populates, and processes a form.
*
......
......@@ -94,7 +94,7 @@ protected function loadCachedFormState($form_build_id, FormStateInterface $form_
$form_state->setFormState($stored_form_state);
// If the original form is contained in include files, load the files.
// @see form_load_include()
// @see \Drupal\Core\Form\FormStateInterface::loadInclude()
$form_state['build_info'] += array('files' => array());
foreach ($form_state['build_info']['files'] as $file) {
if (is_array($file)) {
......
......@@ -61,7 +61,7 @@ class FormState implements FormStateInterface, \ArrayAccess {
* 'name' as needed by module_load_include(). The files listed here are
* automatically loaded by form_get_cache(). By default the current menu
* router item's 'file' definition is added, if any. Use
* form_load_include() to add include files from a form constructor.
* self::loadInclude() to add include files from a form constructor.
* - form_id: Identification of the primary form being constructed and
* processed.
* - base_form_id: Identification for a base form, as declared in the form
......@@ -429,6 +429,29 @@ public function setFormState(array $form_state_additions) {
return $this;
}
/**
* {@inheritdoc}
*/
public function loadInclude($module, $type, $name = NULL) {
if (!isset($name)) {
$name = $module;
}
$build_info = $this->get('build_info');
if (!isset($build_info['files']["$module:$name.$type"])) {
// Only add successfully included files to the form state.
if ($result = $this->moduleLoadInclude($module, $type, $name)) {
$build_info['files']["$module:$name.$type"] = array(
'type' => $type,
'module' => $module,
'name' => $name,
);
$this->set('build_info', $build_info);
return $result;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
......@@ -824,4 +847,11 @@ protected function drupalSetMessage($message = NULL, $type = 'status', $repeat =
return drupal_set_message($message, $type, $repeat);
}
/**
* Wraps ModuleHandler::loadInclude().
*/
protected function moduleLoadInclude($module, $type, $name = NULL) {
return \Drupal::moduleHandler()->loadInclude($module, $type, $name);
}
}
......@@ -46,6 +46,37 @@ public function &getCompleteForm();
*/
public function setCompleteForm(array &$complete_form);
/**
* Ensures an include file is loaded whenever the form is processed.
*
* Example:
* @code
* // Load node.admin.inc from Node module.
* $form_state->loadInclude('inc', 'node', 'node.admin');
* @endcode
*
* Use this function instead of module_load_include() from inside a form
* constructor or any form processing logic as it ensures that the include file
* is loaded whenever the form is processed. In contrast to using
* module_load_include() directly, this method makes sure the include file is
* correctly loaded also if the form is cached.
*
* @param string $module
* The module to which the include file belongs.
* @param string $type
* The include file's type (file extension).
* @param string|null $name
* (optional) The base file name (without the $type extension). If omitted,
* $module is used; i.e., resulting in "$module.$type" by default.
*
* @return string|false
* The filepath of the loaded include file, or FALSE if the include file was
* not found or has been loaded already.
*
* @see module_load_include()
*/
public function loadInclude($module, $type, $name = NULL);
/**
* Returns an array representation of the cacheable portion of the form state.
*
......
......@@ -68,7 +68,7 @@ public function getButtons() {
* @see editor_image_upload_settings_form()
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
form_load_include($form_state, 'inc', 'editor', 'editor.admin');
$form_state->loadInclude('editor', 'admin.inc');
$form['image_upload'] = editor_image_upload_settings_form($editor);
$form['image_upload']['#attached']['library'][] = 'ckeditor/drupal.ckeditor.drupalimage.admin';
$form['image_upload']['#element_validate'][] = array($this, 'validateImageUploadSettings');
......
......@@ -89,7 +89,7 @@ public function form(array $form, FormStateInterface $form_state) {
// - Change $form_state['view'] to $form_state['temporary']['view'].
// - Add a #process function to initialize $form_state['temporary']['view']
// on cached form submissions.
// - Use form_load_include().
// - Use \Drupal\Core\Form\FormStateInterface::loadInclude().
$form_state['no_cache'] = TRUE;
if ($display_id) {
......
......@@ -35,7 +35,7 @@ public function init(FormStateInterface $form_state) {
}
// @todo Remove the need for this.
form_load_include($form_state, 'inc', 'views_ui', 'admin');
$form_state->loadInclude('views_ui', 'inc', 'admin');
$form_state->set('view', $this->entity);
}
......
......@@ -348,6 +348,79 @@ public function providerTestIsValueEmpty() {
return $data;
}
/**
* @covers ::loadInclude
*/
public function testLoadInclude() {
$type = 'some_type';
$module = 'some_module';
$name = 'some_name';
$form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
->setMethods(array('moduleLoadInclude'))
->getMock();
$form_state->expects($this->once())
->method('moduleLoadInclude')
->with($module, $type, $name)
->willReturn(TRUE);
$this->assertTrue($form_state->loadInclude($module, $type, $name));
}
/**
* @covers ::loadInclude
*/
public function testLoadIncludeNoName() {
$type = 'some_type';
$module = 'some_module';
$form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
->setMethods(array('moduleLoadInclude'))
->getMock();
$form_state->expects($this->once())
->method('moduleLoadInclude')
->with($module, $type, $module)
->willReturn(TRUE);
$this->assertTrue($form_state->loadInclude($module, $type));
}
/**
* @covers ::loadInclude
*/
public function testLoadIncludeNotFound() {
$type = 'some_type';
$module = 'some_module';
$form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
->setMethods(array('moduleLoadInclude'))
->getMock();
$form_state->expects($this->once())
->method('moduleLoadInclude')
->with($module, $type, $module)
->willReturn(FALSE);
$this->assertFalse($form_state->loadInclude($module, $type));
}
/**
* @covers ::loadInclude
*/
public function testLoadIncludeAlreadyLoaded() {
$type = 'some_type';
$module = 'some_module';
$name = 'some_name';
$form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
->setConstructorArgs([['build_info' => ['files' => [
'some_module:some_name.some_type' => [
'type' => $type,
'module' => $module,
'name' => $name,
],
]]]])
->setMethods(array('moduleLoadInclude'))
->getMock();
$form_state->expects($this->never())
->method('moduleLoadInclude');
$this->assertFalse($form_state->loadInclude($module, $type, $name));
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment