Version 1
[yaffs-website] / web / modules / contrib / ctools / src / Controller / WizardFormController.php
1 <?php
2
3 namespace Drupal\ctools\Controller;
4
5 use Drupal\Core\Controller\ControllerResolverInterface;
6 use Drupal\Core\Controller\FormController;
7 use Drupal\Core\Form\FormBuilderInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\ctools\Wizard\FormWizardInterface;
10 use Drupal\ctools\Wizard\WizardFactoryInterface;
11 use Symfony\Component\HttpFoundation\Request;
12
13 /**
14  * Wrapping controller for wizard forms that serve as the main page body.
15  */
16 class WizardFormController extends FormController {
17
18   /**
19    * The class resolver.
20    *
21    * @var \Drupal\Core\DependencyInjection\ClassResolverInterface;
22    */
23   protected $classResolver;
24
25   /**
26    * Tempstore Factory for keeping track of values in each step of the wizard.
27    *
28    * @var \Drupal\user\SharedTempStoreFactory
29    */
30   protected $tempstore;
31
32   /**
33    * The event dispatcher.
34    *
35    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
36    */
37   protected $dispatcher;
38
39   /**
40    * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
41    *   The controller resolver.
42    * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
43    *   The form builder.
44    * @param \Drupal\ctools\Wizard\WizardFactoryInterface $wizard_factory
45    *   The wizard factory.
46    */
47   public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder, WizardFactoryInterface $wizard_factory) {
48     parent::__construct($controller_resolver, $form_builder);
49     $this->wizardFactory = $wizard_factory;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function getFormArgument(RouteMatchInterface $route_match) {
56     return $route_match->getRouteObject()->getDefault('_wizard');
57   }
58
59   /**
60    * Wizards are not instantiated as simply as forms, so this method is unused.
61    */
62   protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
63     if (!is_subclass_of($form_arg, '\Drupal\ctools\Wizard\FormWizardInterface')) {
64       throw new \Exception("The _wizard default must reference a class instance of \\Drupal\\ctools\\Wizard\\FormWizardInterface.");
65     }
66     $parameters = $route_match->getParameters()->all();
67     $parameters += $form_arg::getParameters();
68     $parameters['route_match'] = $route_match;
69     return $this->wizardFactory->createWizard($form_arg, $parameters);
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getContentResult(Request $request, RouteMatchInterface $route_match) {
76     $wizard = $this->getFormObject($route_match, $this->getFormArgument($route_match));
77     $ajax = $request->attributes->get('js') == 'ajax' ? TRUE : FALSE;
78
79     return $this->wizardFactory->getWizardForm($wizard, $request->attributes->all(), $ajax);
80   }
81
82 }