0

In Drupal 8, I want to visit a link (e.g. www.domain.com/url-link) which should activate PHP code which is in a module.

Is there a hook which support this? If so, how do I implement this?

  • 1
    I believe Logger should be put to in a seperate question – GiorgosK Mar 10 '18 at 21:44
  • 1
    Once you get an answer for the question you asked, you cannot change the meaning of the question. Questions and answers are not for back-and-forth iterations between the user who asks the question, and the users who answer. – kiamlaluno Mar 10 '18 at 22:25
  • The question is too broad for us. There are at least two ways to implement what you are describing, but even if you were telling us which exactly is your case, we would just end up with reporting here what the documentation on drupal.org and api.drupal.org already says, which is not what we aim to do. Once you are familiar with the concept of routes and event subscribers, and you have troubles with code you are writing, you can ask a question about the specific problem you have with your code. – kiamlaluno Mar 12 '18 at 15:05
  • That's is why I added my code to the question which YOU removed and so then the question get's very general again. – Justme Mar 20 '18 at 10:29
1

Apart from controllers, one could also listen for Events, using code similar to the following. Depending on what the custom logic mentioned in the OP is expected to do, this could be an alternative answer.

nncbt.services.yml:

services:
  nncbt_event_subscriber:
    class: '\Drupal\nncbt\EventSubscriber\NNCBTSubscriber'
    arguments:
      - '@logger.factory'
    tags:
      - { name: 'event_subscriber' }

src/EventSubscriber/NNCBTSubscriber.php:

<?php

namespace Drupal\nncbt\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Drupal\Core\Logger\LoggerChannelFactory;

/**
 * An EventSubscriber for REQUEST and TERMINATE events.
 */
class NNCBTSubscriber implements EventSubscriberInterface {

  /**
   * The logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactory $logger_channel_factory
   *   For logging purposes.
   */
  public function __construct(LoggerChannelFactory $logger_channel_factory) {
    $this->logger = $logger_channel_factory->get(self::class);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['earlyCode'];
    $events[KernelEvents::TERMINATE][] = ['lateCode'];
    return $events;
  }

  /**
   * This method is called for KernelEvents::REQUEST events.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The incoming event.
   */
  public function earlyCode(GetResponseEvent $event) {
    $uri = $event->getRequest()->getRequestUri();
    $this->logger->info($uri);
  }

  /**
   * This method is called for KernelEvents::TERMINATE events.
   *
   * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
   *   The incoming event.
   */
  public function lateCode(PostResponseEvent $event) {
    $uri = $event->getRequest()->getRequestUri();
    $this->logger->info($uri);
  }

}

Good luck!

0

In Drupal 8, you need to create a simple module.

Create the modules/custom/mymodule directory, and put the following files.

mymodule.info.yml

name: Mymodule
type: module
description: Mymodule simple module.
core: 8.x

mymodule.routing.yml

mymodule:
  path: '/urllink'
  defaults:
    _title: 'Page Title'
    _controller: '\Drupal\mymodule\MymoduleController::content'
  requirements:
    _permission: 'access content'

src/MymoduleController.php

<?php
/**
 * @file
 * Contains \Drupal\mymodule\MymoduleController.
 */

namespace Drupal\mymodule;

use Drupal\Core\Controller\ControllerBase;

class MymoduleController extends ControllerBase {
  public function content() {
    //custom logic 
    // ...
    $markup = "custom logic output"; 
    return array(
        '#markup' => $markup,
    );
  }
}

Install the module.

When you visit http://example.com/urllink on your site, the page will display whatever you put in #markup.

  • You wouldn’t really need the module function, that can go into the controller or just become a service later, injected to the constructor. The Controller can also return a render array, #markup or a Symfony Response object. – Kevin Mar 10 '18 at 20:03
  • @Kevin my initial answer was without .module file but realized I should give option to organize code in .module file ... but if you think its bad practice please feel free to edit my answer. – GiorgosK Mar 10 '18 at 20:11
  • Not bad practice per se but yeah I wouldn't mix procedural and OO like that when it can be avoided. It'll nudge in the D8 direction for newcomers. – Kevin Mar 10 '18 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.