isVisibleElement

The isVisibleElement hook is triggered when checking if an element should be visible in the front end or not. An “element” in this case means either an article, a front end module or a content element. In contrast to the other three hooks getArticle, getFrontendModule and getContentElement one can prevent generating the complete markup. The hook passes the model of the instance and the current visibility state as arguments and expects the new visibility state as return value.

Parameters

  1. \Contao\Model $element

    The database result from table tl_article or tl_content or tl_module as a Contao Model object.

  2. boolean $isVisible

    The current visibility state.

Return Values

Add some custom checks and return true, if the element should be visible in the front end. Return false if the element should not be visible in the front end.

Example

// src/EventListener/IsVisibleElementListener.php
namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Model;

#[AsHook('isVisibleElement')]
class IsVisibleElementListener
{
    public function __invoke(Model $element, bool $isVisible): bool
    {
        if ($element instanceof \Contao\ContentModel) {
            // Check if this content element can be shown
            if ($this->myElementCanBeShownInFrontend($element)) {
                return true;
            }
        }

        // Otherwise we don't want to change the visibility state
        return $isVisible;
    }
}

References