Plugins : lien vers le thème actif

cpalocpalo Member
16 janv. modifié dans Plugins

Bonjour,
J'ai un petit plugin qui me permet d'afficher à la fin du un lien vers un fichier css à la racine des thèmes

public function ThemeEndHead() {
        if ($this->getParam('custom_css')!='') {
            echo "\t".'<link rel="stylesheet" href="<?php $plxShow->racine(); ?>themes/'.$this->getParam('custom_folder').''.$this->getParam('custom_css').'">'."\n".PHP_EOL;
        }
    }

je voudrai modifier cette ligne pour le lien pointe sur le thème actif et non pas sur la racine des thèmes.
Cordialemnt

Réponses

  • bazooka07bazooka07 PluXml Lead Developer, Moderator

    Bonjour,
    $plxShow n'est pas directement accessible depuis ton plugin !
    il y a 3 solutions pour y remédier :

    • le déclarer global dans ta fonction
    • utiliser plxShow::getInstance() pour récupérer son pointeur
    • injecter du code PHP dans PluXml avec le hook

    Autre souci. plxShow::template() fournit la valeur que tu cherches. Sauf qu'il affiche directement son résultat. Donc, il faut le capturer avant de l'envoyer à l'écran.

    Voici le code d'un petit plugin qui fait le job demandé :

    <?php
    
    if (!defined('PLX_ROOT')) { exit; }
    
    class kzTest extends plxPlugin {
        const BEGIN_CODE = '<?php # ' . __CLASS__ . ' plugin' . PHP_EOL;
        const END_CODE = PHP_EOL . '?>';
        const HOOKS = array(
            // 'plxMotorPreChauffageBegin',
            // 'plxMotorDemarrageBegin',
            // 'plxMotorDemarrageEnd',
            'ThemeEndHead',
            // 'ThemeEndBody',
        );
    
        public function __construct($default_lang) {
            parent::__construct($default_lang);
            if(!defined('PLX_ADMIN')) {
                foreach(self::HOOKS as $hook) {
                    $this->addHook($hook, $hook);
                }
            }
        }
    
        public function ThemeEndHead() {
            # On force les valeurs des paramètres pour faire un essai
            $this->setParam('custom_css', 'CUSTOM_CSS.css', 'string');
            $this->setParam('custom_folder', 'CUSTOM_FOLDER/', 'string');
    
            if (empty($this->getParam('custom_css'))) {
                return;
            }
            $css = $this->getParam('custom_css');
            $folder = $this->getParam('custom_folder');
            echo self::BEGIN_CODE;
    ?>
    ob_start();
    $plxShow->template();
    $root = ob_get_clean();
    $href = $root . '/<?= $folder ?>' . '<?= $css ?>';
    echo '<link rel="stylesheet" href="' . $href .'" />' . PHP_EOL;
    <?php
            echo self::END_CODE;
        }
    
    }
    
  • bazooka07bazooka07 PluXml Lead Developer, Moderator

    Re,

    Variante plus élégante pour le hook :

        public function ThemeEndHead() {
            # On force les valeurs des paramètres pour faire un essai
            $this->setParam('custom_css', 'CUSTOM_CSS.css', 'string');
            $this->setParam('custom_folder', 'CUSTOM_FOLDER/', 'string');
    
            if (empty($this->getParam('custom_css'))) {
                return;
            }
    ?>
        <link rel="stylesheet" href="<?php
            echo self::BEGIN_CODE;
    ?>
    $plxShow->template();
    <?php
            echo self::END_CODE;
    ?>/<?= $this->getParam('custom_folder') ?><?= $this->getParam('custom_css') ?>" />
    <?php
        }
    
  • bazooka07bazooka07 PluXml Lead Developer, Moderator

    Re, re,
    Je n'aime pas trop utiliser getInstance(). Mais cela simplifie le code :

        public function ThemeEndHead() {
            # On force les valeurs des paramètres pour faire un essai
            $this->setParam('custom_css', 'CUSTOM_CSS.css', 'string');
            $this->setParam('custom_folder', 'CUSTOM_FOLDER/', 'string');
    
            if (empty($this->getParam('custom_css'))) {
                return;
            }
    
            $myShow = plxShow::getInstance();
    ?>
        <link rel="stylesheet" href="<?php $myShow->template(); ?>/<?= $this->getParam('custom_folder') ?><?= $this->getParam('custom_css') ?>" />
    <?php
        }
    

    Ne pas confondre <?php et <?= !!!! ( execute vs affiche )

  • cpalocpalo Member

    Bonjour,
    Merci pour ce retour qui va bien m'aider.
    J'essaie tout ça et te ferait un retour.
    Cordialement

Connectez-vous ou Inscrivez-vous pour répondre.