3 namespace Drupal\drupalmoduleupgrader\Utility;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\Core\StringTranslation\TranslationInterface;
8 use Drupal\drupalmoduleupgrader\TargetInterface;
10 class FormConverterFactory {
12 use StringTranslationTrait;
15 * @var \Drupal\drupalmoduleupgrader\RewriterInterface
19 public function __construct(TranslationInterface $translator, PluginManagerInterface $rewriters) {
20 $this->stringTranslation = $translator;
21 $this->rewriter = $rewriters->createInstance('form_state');
25 * Creates a FormConverter for a specific form.
27 * @param TargetInterface $target
28 * The module which defines the form.
29 * @param string $form_id
30 * The original form ID.
32 * @return FormConverter
34 * @throws \BadMethodCallException if the target module doesn't define
37 public function get(TargetInterface $target, $form_id) {
38 $indexer = $target->getIndexer('function');
40 if ($indexer->has($form_id)) {
41 return new FormConverter($target, $form_id, $this->rewriter);
44 $message = $this->t('@target does not define form @form_id.', [
45 '@target' => $target->id(),
46 '@form_id' => $form_id,
48 throw new \BadMethodCallException($message);