Cerca

Paraules clau suggerides:
Estem perdent trànsit
Estem perdent trànsit

En els últims mesos he observat una disminució del trànsit registrat en diversos webs. Davant d…

Més enllà del DNI:
Més enllà del DNI:

Protegir els menors a Internet és una responsabilitat que planteja reptes importants per a qui…

Riemann i la intel
Riemann i la intel

El 1859, quan encara faltaven dècades perquè apareguessin els primers ordinadors electrònics, el…

Cambiar de Tema según URL o Rol de usuario en Drupal 8

/sites/default/files/itdo-feed/itdo-030dfaf4c3b7604e.png

En el Drupal 7 había algunas opciones sencillas de implementar esta funcionalidad. Tenías el Admin_theme o el themeKey para cambiar de Tema según tu propia regla. Que yo sepa, estos módulos no tienen una versión oficial para el Drupal 8. Así que he tenido que implementar la funcionalidad en un módulo básico personalizado. La interfaz ThemeNegotiatorInterface de Drupal 8 proporciona una forma en el que los desarrolladores pueden crear su propia clase para gestionar el cambio de tema.

Fes-te membre

Rep les últimes novetats directament al teu correu. Sense correu brossa.

Así es como me ha funcionado (solo el código):

mi_modulo.info.yml

name: Mi Modulo
description: 'Switches the active theme based on the users or roles.'
type: module
core: 8.x
mi_modulo.services.yml

services:
  theme.negotiator.mi_modulo:
    class: Drupal\mudar_tema\Theme\MiModuloNegotiator
    tags:
      - { name: theme_negotiator, priority: 10 }
MiModuloNegotitor.php



/**
 * @file
 * Contains \Drupal\mi_modulo\Theme\MiModuloNegotiator.
*/

namespace Drupal\mi_modulo\Theme;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;

class MiModuloNegotiator implements ThemeNegotiatorInterface {

    /**
     * @param RouteMatchInterface $route_match
     * @return bool
     */
    public function applies(RouteMatchInterface $route_match)
    {
        return $this->negotiateRoute($route_match) ? true : false;
    }

    /**
     * @param RouteMatchInterface $route_match
     * @return null|string
     */
    public function determineActiveTheme(RouteMatchInterface $route_match)
    {
        return $this->negotiateRoute($route_match) ?: null;
    }

    /**
     * Función responsable por seleccionar un tema
     * @param RouteMatchInterface $route_match
     * @return bool|string
     */
    private function negotiateRoute(RouteMatchInterface $route_match)
    {
        $userRolesArray = \Drupal::currentUser()->getRoles();

        if (in_array("developer", $userRolesArray))
        {
            return 'seven';
        }

        return false;
    }
}
Si tienes alguna duda o pregunta, no dudes ni un minuto ;)
Comentaris
Comentario