Skip to content

Instantly share code, notes, and snippets.

@zviryatko
Created March 17, 2016 08:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zviryatko/37c01dcd2e24ae1674f7 to your computer and use it in GitHub Desktop.
Save zviryatko/37c01dcd2e24ae1674f7 to your computer and use it in GitHub Desktop.
Hide forgot password links in Drupal 8.
name: Custom User
type: module
description: Provide user specific functionality
core: 8.x
package: Custom
services:
custom_user.route_subscriber:
class: Drupal\custom_user\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
custom_user_pass_path_processor:
class: Drupal\custom_user\PathProcessor\UserPassPathProcessor
tags:
- { name: path_processor_outbound, priority: 201 }
<?php
/**
* @file
* Contains \Drupal\custom_user\Routing\RouteSubscriber.
*
* Put this file in "src/Routing" module directory.
*/
namespace Drupal\custom_user\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events and restrict access to user.pass route.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('user.pass')) {
$route->setRequirement('_access', 'FALSE');
}
}
}
<?php
/**
* @file
* Contains \Drupal\custom_user\PathProcessor\UserPassProcessor.
*
* Put this file in "src/PathProcessor" module directory.
*/
namespace Drupal\custom_user\PathProcessor;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
/**
* Processes the outbound path and replace user.pass route to email link.
*/
class UserPassPathProcessor implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if ($path === '/user/password') {
$site_email = \Drupal::config('system.site')->get('mail');
$url = Url::fromUri('mailto:' . $site_email)->setOption('query', [
'subject' => t('Request new password'),
'body' => t('I forgot my password, please, reset it for me, my user email is: :email', [':email' => empty($options['query']['name']) ? '' : $options['query']['name']]),
]);
$path = $url->toString();
// Fix useless param in query.
if (!empty($options['query']['name'])) {
$path .= '&';
}
}
return $path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment