public function EntityFormDisplay::getWidget

Returns the Widget plugin for a field.

Parameters

string $field_name: The field name.

Return value

\Drupal\field\Plugin\Type\Widget\WidgetInterface|null A Widget plugin or NULL if the field does not exist.

Overrides EntityFormDisplayInterface::getWidget

File

drupal/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php, line 48
Contains \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay.

Class

EntityFormDisplay
Configuration entity that contains widget options for all components of a entity form in a given form mode.

Namespace

Drupal\entity\Plugin\Core\Entity

Code

public function getWidget($field_name) {
  if (isset($this->plugins[$field_name])) {
    return $this->plugins[$field_name];
  }

  // Instantiate the widget object from the stored display properties.
  if ($configuration = $this
    ->getComponent($field_name)) {
    $instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle);
    $widget = $this->pluginManager
      ->getInstance(array(
      'instance' => $instance,
      'form_mode' => $this->originalMode,
      // No need to prepare, defaults have been merged in setComponent().
      'prepare' => FALSE,
      'configuration' => $configuration,
    ));
  }
  else {
    $widget = NULL;
  }

  // Persist the widget object.
  $this->plugins[$field_name] = $widget;
  return $widget;
}