function FormState::loadInclude

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::loadInclude()
  2. 8.9.x core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::loadInclude()
  3. 11.x core/lib/Drupal/Core/Form/FormState.php \Drupal\Core\Form\FormState::loadInclude()

Ensures an include file is loaded whenever the form is processed.

Example:

// Load node.admin.inc from Node module.
$form_state->loadInclude('node', 'inc', 'node.admin');

Use this function instead of \Drupal::moduleHandler()->loadInclude() 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 \Drupal::moduleHandler()->loadInclude() directly, this method makes sure the include file is correctly loaded also if the form is cached.

Parameters

string $module: The module to which the include file belongs.

string $type: The include file's type (file extension).

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 value

string|false The filepath of the loaded include file, or FALSE if the include file was not found or has been loaded already.

Overrides FormStateInterface::loadInclude

File

core/lib/Drupal/Core/Form/FormState.php, line 904

Class

FormState
Stores information about the state of a form.

Namespace

Drupal\Core\Form

Code

public function loadInclude($module, $type, $name = NULL) {
  if (!isset($name)) {
    $name = $module;
  }
  $build_info = $this->getBuildInfo();
  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}"] = [
        'type' => $type,
        'module' => $module,
        'name' => $name,
      ];
      $this->setBuildInfo($build_info);
      return $result;
    }
  }
  return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.