You are here

class RenameAdminPathsProcessor in Rename Admin Paths 8

Path processor for url_alter_test.

Hierarchy

Expanded class hierarchy of RenameAdminPathsProcessor

1 string reference to 'RenameAdminPathsProcessor'
rename_admin_paths.services.yml in ./rename_admin_paths.services.yml
rename_admin_paths.services.yml
1 service uses RenameAdminPathsProcessor
rename_admin_paths.rename_admin_paths_processor in ./rename_admin_paths.services.yml
Drupal\rename_admin_paths\RenameAdminPathsProcessor

File

src/RenameAdminPathsProcessor.php, line 13

Namespace

Drupal\rename_admin_paths
View source
class RenameAdminPathsProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {

  /**
   * Implements Drupal\Core\PathProcessor\InboundPathProcessorInterface::processInbound().
   */
  public function processInbound($path, Request $request) {
    $config = \Drupal::config('rename_admin_paths.settings');

    // Admin path.
    if ($config
      ->get('admin_path')) {
      $admin_path_value = $config
        ->get('admin_path_value');

      // 404 for default admin path.
      if (preg_match('|^/admin(?![^/])|i', $path)) {
        $path = '/404';
      }
      elseif (preg_match('|^/' . urlencode($admin_path_value) . '(?![^/])(.*)|', $path, $matches)) {
        $path = '/admin' . $matches[1];
      }
    }

    // User path.
    if ($config
      ->get('user_path')) {
      $user_path_value = $config
        ->get('user_path_value');

      // 404 for default user path.
      if (preg_match('|^/user(?![^/])|i', $path)) {
        $path = '/404';
      }
      elseif (preg_match('|^/' . urlencode($user_path_value) . '(?![^/])(.*)|', $path, $matches)) {
        $path = '/user' . $matches[1];
      }
    }
    return $path;
  }

  /**
   * Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound().
   */
  public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    $config = \Drupal::config('rename_admin_paths.settings');

    // Admin path.
    if ($request && $config
      ->get('admin_path')) {
      $admin_path_value = $config
        ->get('admin_path_value');

      // Replace admin in path.
      if (preg_match('|^/admin(?![^/])(.*)|', $path, $matches)) {
        $path = '/' . urlencode($admin_path_value) . $matches[1];
      }
    }

    // User path.
    if ($config
      ->get('user_path')) {
      $user_path_value = $config
        ->get('user_path_value');

      // Replace user in path.
      if (preg_match('|^/user(?![^/])(.*)|', $path, $matches)) {
        $path = '/' . urlencode($user_path_value) . $matches[1];
      }
    }
    return $path;
  }

}

Members