[1640 Aufrufe]

5.4 Hooks: backend

In diesem Abschnitt beschäftigen wir uns mit den Hooks für das Backend.

Hook: addLogEntry

Dieser Hook ist veraltet und kann unter Contao 5 nicht mehr verwendet werden!

Der addLogEntry Hook wird aufgerufen, wenn ein neuer Eintrag in das Contao-Log geschrieben wird. Es wird kein Rückgabewert erwartet.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: addLogEntry, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(string $message, string $func, string $action): void
    {
        // Do something …
    }
}

Parameter:

Parameter Typ Beschreibung
$message string Inhalt der Log-Message
$func string Name der Funktion, die den Eintrag erstellt
$action string Name der Aktion, die den Eintrag verursacht

Werte für $action:

  • TL_ERROR
  • TL_ACCESS
  • TL_GENERAL
  • TL_FILES
  • TL_CRON
  • TL_FORMS
  • TL_CONFIGURATION
  • TL_NEWSLETTER
  • TL_REPOSITORY
  • Jeder beliebige String

Rückgabewert:

keiner

Referenz im Contao Core: \Contao\CoreBundle\Monolog\ContaoTableHandler#L158-L160

Hook: colorizeLogEntries

Der colorizeLogEntries Hook wird aufgerufen, wenn ein Eintrag des Contao-Logs angezeigt wird. Es wird der Inhalt aus Rückgabe erwartet.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: colorizeLogEntries, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(array $row, string $label): string
    {
        // Wrap the label with a span containing a custom CSS class or style attributes
        if (true) {
            $label = preg_replace('@^(.*</span> )(.*)$@U', '$1 <span class="tl_purple">$2</span>', $label);
        }

        return $label;
    }
}

Parameter:

Parameter Typ Beschreibung
$row array Daten des Eintrags aus tl_log
$label string Der von Contao erstellte Inhalt

Rückgabewert:

Typ Beschreibung
string Der modifizierte Inhalt

Referenz im Contao Core: \tl_log#L93-L101

Hook: getSystemMessages

Mit dem getSystemMessages Hook können Meldungen im Backend von Contao ausgegeben werden.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: getSystemMessages, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(): string
    {
        // Display a warning if the system admin's email is not set
        if (empty($GLOBALS['TL_ADMIN_EMAIL'])) {
            return '<p class="tl_error">Please add your email address to system settings.</p>';
        }

        return '';
    }
}

Parameter:

keine

Rückgabewert:

Typ Beschreibung
string Der Inhalt der Meldung

Referenz im Contao Core: \Contao\Backend#L918-L949

Hook: getUserNavigation

Mit dem getUserNavigation Hook kann die Navigation im Backend modifiziert werden.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: getUserNavigation, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(array $modules, bool $showAll): array
    {
        // Add custom navigation item to the Contao website
        $modules['system']['modules']['contao'] = [
            'label' => 'Contao homepage',
            'title' => 'Visit the Contao CMS website',
            'class' => 'navigation contao',
            'href' => 'https://contao.org/en/',
        ];

        return $modules;
    }
}

Parameter:

Parameter Typ Beschreibung
$modules array Array mit einer Liste der Backend Module
$showAll bool Entscheidet, ob alle Module angezeigt werden sollen, auch wenn die Gruppe minimiert ist.

Rückgabewert:

Typ Beschreibung
array Array mit einer Liste der Backend Module

Referenz im Contao Core: \Contao\BackendUser#L538-L546

Hook: isAllowedToEditComment

Mit dem isAllowedToEditComment Hook kann eine Überprüfung der Rechte für das Bearbeiten eines Kommentars aus einer unbekannten Quelle vorgenommen werden.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: isAllowedToEditComment, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(int $parentId, string $parentTable): bool
    {
        // Check the access to your custom module
        if (\Contao\BackendUser::getInstance()->hasAccess('custom', 'modules')) {
            return true;
        }

        return false;
    }
}

Parameter:

Parameter Typ Beschreibung
$parentId int Id der Quelle des Kommentars
$parentTable string Name der Elterntabelle des Kommentars

Rückgabewert:

Typ Beschreibung
bool Wird true zurückgegeben, ist die Bearbeitung erlaubt, bei falsenicht

Referenz im Contao Core: \tl_comments#L457-L472

Hook: listComments

Der listComments Hook wird aufgerufen, wenn Kommentare einer unbekannten Quelle angezeigt werden sollen.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: listComments, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(array $comment): string
    {
        if ('tl_mytable' === $comment['source']) {
            return '<a href="contao/main.php?do=comments&id=' . $comment['id'] . '">' . $comment['title'] . '</a>';
        }

        return '';
    }
}

Parameter:

Parameter Typ Beschreibung
$comment array Array mit den Daten des Kommentars aus tl_comments

Rückgabewert:

Typ Beschreibung
string Der Link für die Bearbeitung des Kommentars

Referenz im Contao Core: \tl_comments.php#L558-L573

Hook: postUpload

Der postUpload Hook wird aufgerufen, wenn ein Nutzer eine oder mehrere Dateien im Backend hochgeladen hat.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: postUpload, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(array $files): void
    {
        // Do something …
    }
}

Parameter:

Parameter Typ Beschreibung
$files array Array mit den hochgeladenen Dateien

Rückgabewert:

keiner

Referenz im Contao Core: \Contao\DC_Folder#L1140-L1155

Hook: reviseTable

Der reviseTable Hook wird aufgerufen, wenn Contao die Datenbank bereinigt. Es werden dann veraiste Datensätze entfernt.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: reviseTable, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;


class ExampleHook {

    public function handleHook(string $table, ?array $newRecords, ?string $parentTable, ?array $childTables): ?bool
    {
        // Do something …
    }
}

Parameter:

Parameter Typ Beschreibung
$table string Name der Tabelle die gerade bearbeiet wird
$newRecords array|null Array mit den Ids der neuen Datensätze
$parentTable string|null Name der Elterntabelle
$childTables array|null Array mit den Name der Kindtabellen

Rückgabewert:

Typ Beschreibung
bool|null Bei true wird die Seite neugeladen, bei false oder null nicht

Referenz im Contao Core: \Contao\DC_Table#L3284-L3306

Hook: setNewPassword

Der setNewPassword Hook wird aufgerufen, nachdem ein neues Passwort gesetzt wurde.

Registrierung

# /src/Ctocb/Example/Resources/config/services.yml
services:

  # Hooks
  Ctocb\Example\Classes\Contao\Hooks\ExampleHook:
    public: true
    tags:
      - { name: contao.hook, hook: setNewPassword, method: handleHook, priority: 1024 }

Klasse

<?php

namespace Ctocb\Example\Classes\Contao\Hooks;

use Contao\Module;

class ExampleHook {

    public function handleHook($member, string $password, Module $module = null): void
    {
        // Do something …
    }
}

Parameter:

Parameter Typ Beschreibung
$member \Contao\Database\Result| \Contao\MemberModel Die Daten des Mitglieds, dass sein Passwort geändert hat
$password string Das verschlüsselte Passwort
$module \Contao\Module|null Instanz des Forntend Moduls, aus dem die Änderung veranlasst wurde

Rückgabewert:

keiner

Referenz im Contao Core: \tl_member#L537-L544, \Contao\ModuleChangePassword.php#L178-L186 und \Contao\ModulePassword#L266-L274