Commit 38b64382 authored by Emmanuel Libbrecht's avatar Emmanuel Libbrecht
Browse files

Issue #3324803: Add optional parameters for indiviual routes

parent 08d4c5af
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -49,18 +49,19 @@ function route_in_modal_link_alter(&$variables) {

  /** @var \Drupal\route_in_modal\ModalRouteHelper $modalRouteHelper */
  $modalRouteHelper = \Drupal::service('route_in_modal.helper');
  if (!$modalRouteHelper->routeHasModal($routeName)) {
  $routeHasModal = $modalRouteHelper->routeHasModal($routeName);
  if ($routeHasModal === FALSE) {
    return;
  }

  $config = \Drupal::config('route_in_modal.settings');

  $dialogWidth = $config->get('dialog_width');
  $dialogWidth = $routeHasModal['width'] ?? $config->get('dialog_width');
  if (empty($dialogWidth) && $dialogWidth !== '0') {
    $dialogWidth = '480';
  }

  $dialogHeight = $config->get('dialog_height');
  $dialogHeight = $routeHasModal['height'] ?? $config->get('dialog_height');
  if (empty($dialogHeight) && $dialogHeight !== '0') {
    $dialogHeight = 'auto';
  }
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ class RouteInModalSettingsForm extends ConfigFormBase {
    $form['routes'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Add routes to be opened in a modal'),
      '#description' => $this->t('One route per line'),
      '#description' => $this->t('One route per line. Optional parameters can be added by separating the route with a pipe sign. Individual parameters should be separated with a comma sign. e.g. |width:480,height:auto'),
      '#default_value' => $config->get('routes'),
    ];

+43 −5
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ class ModalRouteHelper {
   * @return array
   *   The configured routes as array.
   */
  public function getRoutes(): array {
  private function getRoutes(): array {
    $routes = $this->config->get('routes');

    return (empty($routes) ? [] : explode("\r\n", $routes));
@@ -44,11 +44,49 @@ class ModalRouteHelper {
   * @param string|null $route
   *   The route to check.
   *
   * @return bool
   *   True if route is configured to open in modal, false otherwise.
   * @return false|array
   *   The route if the route is configured to open in modal, false otherwise.
   */
  public function routeHasModal(?string $route): bool {
    return in_array($route, $this->getRoutes(), TRUE);
  public function routeHasModal(?string $route) {
    $configuredRoutes = $this->getRoutes();

    foreach ($configuredRoutes as $configuredRoute) {
      $extractedRoute = $this->extractRoute($configuredRoute);

      if ($route == $extractedRoute['route']) {
        return $extractedRoute['parameters'];
      }
    }

    return FALSE;
  }

  /**
   * Extract a route to the correct format to make use of parameters.
   *
   * @param string|null $route
   *   The route to extract.
   *
   * @return array
   *   The extracted route with the route and optional parameters.
   */
  private function extractRoute(?string $route): array {
    $parameters = [];

    if (strpos($route, '|')) {
      $routeParameters = explode(',', substr($route, strrpos($route, '|') + 1));

      foreach ($routeParameters as $routeParameter) {
        if (strpos($routeParameter, ':') !== FALSE) {
          [$key, $value] = explode(':', $routeParameter);
          $parameters[$key] = $value;
        }
      }

      $route = substr($route, 0, strrpos($route, '|'));
    }

    return ['route' => $route, 'parameters' => $parameters];
  }

}