Commit a4c569f3 authored by Aldo's avatar Aldo

routing 1

parent 2227c0a6
<?php
/**
* @file
* Install, update and uninstall functions for the New Route module.
*/
/**
* Implements hook_install().
*/
function new_route_install() {
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_uninstall().
*/
function new_route_uninstall() {
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_schema().
*/
function new_route_schema() {
$schema['new_route_example'] = [
'description' => 'Table description.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique record ID.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {users}.uid of the user who created the record.',
],
'status' => [
'description' => 'Boolean indicating whether this record is active.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
],
'type' => [
'type' => 'varchar_ascii',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Type of the record.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp when the record was created.',
],
'data' => [
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
'description' => 'The arbitrary data for the item.',
],
],
'primary key' => ['id'],
'indexes' => [
'type' => ['type'],
'uid' => ['uid'],
'status' => ['status'],
],
];
return $schema;
}
/**
* Implements hook_requirements().
*/
function new_route_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$value = mt_rand(0, 100);
$requirements['new_route_status'] = [
'title' => t('New Route status'),
'value' => t('New Route value: @value', ['@value' => $value]),
'severity' => $value > 50 ? REQUIREMENT_INFO : REQUIREMENT_WARNING,
];
}
return $requirements;
}
administer new_route configuration:
title: 'Administer new_route configuration'
description: 'Optional description.'
restrict access: true
new_route.example:
path: '/new-route/example'
defaults:
_title: 'Example'
_controller: '\Drupal\new_route\Controller\NewRouteController::build'
requirements:
_permission: 'access content'
new_route.settings_form:
path: '/admin/config/system/new-route'
defaults:
_title: 'New Route settings'
_form: 'Drupal\new_route\Form\SettingsForm'
requirements:
_permission: 'administer new_route configuration'
services:
new_route.event_subscriber:
class: Drupal\new_route\EventSubscriber\NewRouteSubscriber
new_route.route_subscriber:
class: Drupal\new_route\Routing\RouteSubscriber
arguments: ['@messenger']
tags:
- { name: event_subscriber }
......
<?php
namespace Drupal\new_route\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Returns responses for New Route routes.
*/
class NewRouteController extends ControllerBase {
/**
* Builds the response.
*/
public function build() {
$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];
return $build;
}
}
<?php
namespace Drupal\new_route\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure New Route settings for this site.
*/
class SettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'new_route_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['new_route.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['example'] = [
'#type' => 'textfield',
'#title' => $this->t('Example'),
'#default_value' => $this->config('new_route.settings')->get('example'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('example') != 'example') {
$form_state->setErrorByName('example', $this->t('The value is not correct.'));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('new_route.settings')
->set('example', $form_state->getValue('example'))
->save();
parent::submitForm($form, $form_state);
}
}
<?php
namespace Drupal\new_route\EventSubscriber;
namespace Drupal\new_route\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
......@@ -10,7 +10,7 @@ use Symfony\Component\Routing\RouteCollection;
/**
* New Route event subscriber.
*/
class NewRouteSubscriber extends RouteSubscriberBase
class RouteSubscriber extends RouteSubscriberBase
{
/**
......@@ -25,6 +25,7 @@ class NewRouteSubscriber extends RouteSubscriberBase
// Always deny access to '/user/logout'.
// Note that the second parameter of setRequirement() is a string.
if ($route = $collection->get('user.logout')) {
$route->setPath('/logout');
$route->setRequirement('_access', 'TRUE');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment