You are here

public static function CacheExampleForm::create in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 cache_example/src/Form/CacheExampleForm.php \Drupal\cache_example\Form\CacheExampleForm::create()

Instantiates a new instance of this class.

This is a factory method that returns a new instance of this class. The factory should pass any needed dependencies into the constructor of this class, but not the container itself. Every call to this method must return a new instance of this class; that is, it may not implement a singleton.

Parameters

\Symfony\Component\DependencyInjection\ContainerInterface $container: The service container this instance should use.

Overrides FormBase::create

File

modules/cache_example/src/Form/CacheExampleForm.php, line 47

Class

CacheExampleForm
Form with examples on how to use cache.

Namespace

Drupal\cache_example\Form

Code

public static function create(ContainerInterface $container) {

  // Forms that require a Drupal service or a custom service should access
  // the service using dependency injection.
  // @link https://www.drupal.org/node/2203931.
  // Those services are passed in the $container through the static create
  // method.
  $form = new static();
  $form
    ->setRequestStack($container
    ->get('request_stack'))
    ->setStringTranslation($container
    ->get('string_translation'))
    ->setMessenger($container
    ->get('messenger'));
  $form->currentUser = $container
    ->get('current_user');
  $form->cacheBackend = $container
    ->get('cache.default');
  $form->dateFormatter = $container
    ->get('date.formatter');
  $form->fileSystem = $container
    ->get('file_system');
  return $form;
}