Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Utility / FormConverterFactory.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Utility;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\Core\StringTranslation\TranslationInterface;
8 use Drupal\drupalmoduleupgrader\TargetInterface;
9
10 class FormConverterFactory {
11
12   use StringTranslationTrait;
13
14   /**
15    * @var \Drupal\drupalmoduleupgrader\RewriterInterface
16    */
17   protected $rewriter;
18
19   public function __construct(TranslationInterface $translator, PluginManagerInterface $rewriters) {
20     $this->stringTranslation = $translator;
21     $this->rewriter = $rewriters->createInstance('form_state');
22   }
23
24   /**
25    * Creates a FormConverter for a specific form.
26    *
27    * @param TargetInterface $target
28    *  The module which defines the form.
29    * @param string $form_id
30    *  The original form ID.
31    *
32    * @return FormConverter
33    *
34    * @throws \BadMethodCallException if the target module doesn't define
35    * the given form.
36    */
37   public function get(TargetInterface $target, $form_id) {
38     $indexer = $target->getIndexer('function');
39
40     if ($indexer->has($form_id)) {
41       return new FormConverter($target, $form_id, $this->rewriter);
42     }
43     else {
44       $message = $this->t('@target does not define form @form_id.', [
45         '@target' => $target->id(),
46         '@form_id' => $form_id,
47       ]);
48       throw new \BadMethodCallException($message);
49     }
50   }
51
52 }