You are here

class FormRoute in Drupal 7 to 8/9 Module Upgrader 8

Plugin annotation


@Converter(
 id = "drupal_get_form",
 description = @Translation("Converts a drupal_get_form() menu item to a _form route."),
 dependencies = { "router.route_provider", "plugin.manager.drupalmoduleupgrader.rewriter", "drupalmoduleupgrader.form_converter" }
)

Hierarchy

Expanded class hierarchy of FormRoute

File

src/Plugin/DMU/Routing/FormRoute.php, line 21

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Routing
View source
class FormRoute extends ContentRoute {

  /**
   * @var \Drupal\drupalmoduleupgrader\Utility\FormConverter
   */
  protected $formConverter;
  public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, RouteProviderInterface $route_provider, PluginManagerInterface $rewriters, FormConverterFactory $form_converter) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log, $route_provider, $rewriters);
    $this->formConverter = $form_converter;
  }

  /**
   * {@inheritdoc}
   */
  public function getName(TargetInterface $target, RouteWrapper $route) {
    $name = $target
      ->id() . '.' . $this
      ->unPrefix($route['page arguments'][0], $target
      ->id());
    $arguments = array_filter(array_slice($route['page arguments'], 1), 'is_string');
    if ($arguments) {
      $name .= '_' . implode('_', $arguments);
    }
    return $name;
  }

  /**
   * {@inheritdoc}
   */
  protected function buildParameterMap(TargetInterface $target, RouteWrapper $route) {
    $map = parent::buildParameterMap($target, $route);
    $indexer = $target
      ->getIndexer('function');
    if ($indexer
      ->has($route['page arguments'][0])) {
      $builder = $indexer
        ->get($route['page arguments'][0]);
      $parameters = $this
        ->bumpKeys(array_slice($builder
        ->getParameters()
        ->toArray(), 2), 2);
      $arguments = $this
        ->bumpKeys(array_slice($route['page arguments'], 1), 2);
      $map
        ->merge(new ParameterMap($route
        ->getPath(), $parameters, $arguments));
    }
    return $map;
  }

  /**
   * Returns a copy of the input array with the keys increased by $offset. This
   * only works on numerically indexed arrays; I don't know what it does to
   * associative arrays, but probably nothing good.
   *
   * @param array $input
   *   The input array.
   *
   * @param int $offset
   *   The offset to add to the keys.
   *
   * @return array
   */
  private function bumpKeys(array $input, $offset = 0) {
    $output = [];
    foreach ($input as $key => $value) {
      $output[$key + $offset] = $value;
    }
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRoute(TargetInterface $target, RouteWrapper $route) {
    $controller = $this->formConverter
      ->get($target, $route['page arguments'][0])
      ->build();
    $target
      ->getIndexer('class')
      ->addFile($this
      ->writeClass($target, $controller));
  }
  protected function getController(TargetInterface $target, RouteWrapper $route) {
    return $this->formConverter
      ->get($target, $route['page arguments'][0])
      ->render();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRouteDefinition(TargetInterface $target, RouteWrapper $route) {
    $definition = parent::buildRouteDefinition($target, $route);
    $definition
      ->setDefault('_form', $this
      ->getController($target, $route)
      ->getName()
      ->getAbsolutePath());
    return $definition;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentRoute::$rewriters protected property
ContentRoute::$routeProvider protected property
ContentRoute::buildPath public function Builds the Drupal 8 path for the route. Overrides RouteConverterInterface::buildPath
ContentRoute::convert final public function Conform with ConverterInterface, which we implement through ConverterBase. Because route conversion is so complex, the Routing plugin never calls this method. It relies instead on the other methods defined in RouteConverterInterface. Overrides ConverterInterface::convert
ConverterBase::buildFixMe protected function Builds a FIXME notice using either the text in the plugin definition, or passed-in text.
ConverterBase::DOC_COMMENT constant
ConverterBase::executeHook protected function Executes the target module's implementation of the specified hook, and returns the result.
ConverterBase::implement protected function Creates an empty implementation of a hook.
ConverterBase::isExecutable public function Returns if this conversion applies to the target module. If FALSE, the convert() method will not be called. Overrides ConverterInterface::isExecutable 4
ConverterBase::LINE_COMMENT constant
ConverterBase::parse protected function Parses a generated class into a syntax tree.
ConverterBase::rewriteFunction protected function Parametrically rewrites a function.
ConverterBase::write public function Writes a file to the target module's directory.
ConverterBase::writeClass public function Writes a class to the target module's PSR-4 root.
ConverterBase::writeInfo protected function Writes out arbitrary data in YAML format.
ConverterBase::writeService protected function Writes a service definition to the target module's services.yml file.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormRoute::$formConverter protected property
FormRoute::buildParameterMap protected function Builds a parameter map from the aggregated arguments of the title, access, and page callbacks. Overrides ContentRoute::buildParameterMap
FormRoute::buildRoute public function Builds the Drupal 8 route, making any needed changes to the original module and/or callback. Overrides ContentRoute::buildRoute
FormRoute::buildRouteDefinition public function Builds the Drupal 8 definition for the route, without making any changes to the original module or callback. Overrides ContentRoute::buildRouteDefinition
FormRoute::bumpKeys private function Returns a copy of the input array with the keys increased by $offset. This only works on numerically indexed arrays; I don't know what it does to associative arrays, but probably nothing good.
FormRoute::getController protected function Overrides ContentRoute::getController
FormRoute::getName public function Generates the route's machine-readable name. Overrides ContentRoute::getName
FormRoute::__construct public function Constructs a RouteConverterBase object. Overrides ContentRoute::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$log protected property
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTransformTrait::deleteLegacyWildcards public function Deletes %wildcards from a route path.
StringTransformTrait::deleteWildcards public function Deletes {wildcards} from a route path.
StringTransformTrait::getIdentifier public function Generates an identifier (prefixed with the module name, if $this->module exists) from an arbitrary string.
StringTransformTrait::getIdentifierFromLegacyPath public function Generates an identifier from a Drupal 7 path.
StringTransformTrait::getIdentifierFromPath public function Generates an identifier from a path.
StringTransformTrait::toCamelCase public function Converts a string toCamelCase :)
StringTransformTrait::toTitleCase public function Converts a string ToTitleCase.
StringTransformTrait::unPrefix public function Trims a prefix (as well as leading or trailing underscore, if any) from a string.
StringTransformTrait::unSuffix public function Trims a suffix (as well as leading underscore, if any) from a string.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.