Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / PluginFormFactory.php
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6 use Drupal\Component\Plugin\PluginAwareInterface;
7 use Drupal\Core\DependencyInjection\ClassResolverInterface;
8
9 /**
10  * Provides form discovery capabilities for plugins.
11  */
12 class PluginFormFactory implements PluginFormFactoryInterface {
13
14   /**
15    * The class resolver.
16    *
17    * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
18    */
19   protected $classResolver;
20
21   /**
22    * PluginFormFactory constructor.
23    *
24    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
25    *   The class resolver.
26    */
27   public function __construct(ClassResolverInterface $class_resolver) {
28     $this->classResolver = $class_resolver;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function createInstance(PluginWithFormsInterface $plugin, $operation, $fallback_operation = NULL) {
35     if (!$plugin->hasFormClass($operation)) {
36       // Use the default form class if no form is specified for this operation.
37       if ($fallback_operation && $plugin->hasFormClass($fallback_operation)) {
38         $operation = $fallback_operation;
39       }
40       else {
41         throw new InvalidPluginDefinitionException($plugin->getPluginId(), sprintf('The "%s" plugin did not specify a "%s" form class', $plugin->getPluginId(), $operation));
42       }
43     }
44
45     $form_class = $plugin->getFormClass($operation);
46
47     // If the form specified is the plugin itself, use it directly.
48     if (ltrim(get_class($plugin), '\\') === ltrim($form_class, '\\')) {
49       $form_object = $plugin;
50     }
51     else {
52       $form_object = $this->classResolver->getInstanceFromDefinition($form_class);
53     }
54
55     // Ensure the resulting object is a plugin form.
56     if (!$form_object instanceof PluginFormInterface) {
57       throw new InvalidPluginDefinitionException($plugin->getPluginId(), sprintf('The "%s" plugin did not specify a valid "%s" form class, must implement \Drupal\Core\Plugin\PluginFormInterface', $plugin->getPluginId(), $operation));
58     }
59
60     if ($form_object instanceof PluginAwareInterface) {
61       $form_object->setPlugin($plugin);
62     }
63
64     return $form_object;
65   }
66
67 }