Class luya\cms\frontend\events\MenuItemEvent

Inheritanceluya\cms\frontend\events\MenuItemEvent » yii\base\Event » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-cms/blob/master/src/frontend/events/MenuItemEvent.php

Event after a menu item is loaded and ready.

Each Menu Item does have en event which will be trigger after create. You can also access this event in your config like this for instance:

'menu' => [
    'class' => 'luya\cms\Menu',
    'on eventOnItemFind' => function(MenuItemEvent $event) {
        if ($event->item->alias == 'this-is-my-alias') {
            // will turn this item to invisble
            $event->visible = false;
        }
    }
 ],

Public Properties

Hide inherited properties

Property Type Description Defined By
$data mixed The data that is passed to yii\base\Component::on() when attaching an event handler. yii\base\Event
$handled boolean Whether the event is handled. yii\base\Event
$item luya\cms\menu\Item luya\cms\frontend\events\MenuItemEvent
$name string The event name. yii\base\Event
$sender object|null The sender of this event. yii\base\Event
$visible integer Menu item visibility getter/setter. luya\cms\frontend\events\MenuItemEvent

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
getVisible() Whether the item is visible or not luya\cms\frontend\events\MenuItemEvent
hasHandlers() Returns a value indicating whether there is any handler attached to the specified class-level event. yii\base\Event
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() Detaches an event handler from a class-level event. yii\base\Event
offAll() Detaches all registered class-level event handlers. yii\base\Event
on() Attaches an event handler to a class-level event. yii\base\Event
setVisible() Setter method for the item visibility luya\cms\frontend\events\MenuItemEvent
trigger() Triggers a class-level event. yii\base\Event

Property Details

Hide inherited properties

$item public property
public luya\cms\menu\Item $item null
$visible public property

Menu item visibility getter/setter.

public integer $visible null

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
getVisible() public method

Whether the item is visible or not

public boolean getVisible ( )

                public function getVisible()
{
    return !$this->item->isHidden;
}

            
hasHandlers() public static method

Defined in: yii\base\Event::hasHandlers()

Returns a value indicating whether there is any handler attached to the specified class-level event.

Note that this method will also check all parent classes to see if there is any handler attached to the named event.

public static boolean hasHandlers ( $class, $name )
$class string|object

The object or the fully qualified class name specifying the class-level event.

$name string

The event name.

return boolean

Whether there is any handler attached to the event.

                public static function hasHandlers($class, $name)
{
    if (empty(self::$_eventWildcards) && empty(self::$_events[$name])) {
        return false;
    }
    if (is_object($class)) {
        $class = get_class($class);
    } else {
        $class = ltrim($class, '\\');
    }
    $classes = array_merge(
        [$class],
        class_parents($class, true),
        class_implements($class, true)
    );
    // regular events
    foreach ($classes as $className) {
        if (!empty(self::$_events[$name][$className])) {
            return true;
        }
    }
    // wildcard events
    foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
        if (!StringHelper::matchWildcard($nameWildcard, $name, ['escape' => false])) {
            continue;
        }
        foreach ($classHandlers as $classWildcard => $handlers) {
            if (empty($handlers)) {
                continue;
            }
            foreach ($classes as $className) {
                if (StringHelper::matchWildcard($classWildcard, $className, ['escape' => false])) {
                    return true;
                }
            }
        }
    }
    return false;
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
off() public static method

Defined in: yii\base\Event::off()

Detaches an event handler from a class-level event.

This method is the opposite of on().

Note: in case wildcard pattern is passed for class name or event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.

See also on().

public static boolean off ( $class, $name, $handler null )
$class string

The fully qualified class name from which the event handler needs to be detached.

$name string

The event name.

$handler callable|null

The event handler to be removed. If it is null, all handlers attached to the named event will be removed.

return boolean

Whether a handler is found and detached.

                public static function off($class, $name, $handler = null)
{
    $class = ltrim($class, '\\');
    if (empty(self::$_events[$name][$class]) && empty(self::$_eventWildcards[$name][$class])) {
        return false;
    }
    if ($handler === null) {
        unset(self::$_events[$name][$class]);
        unset(self::$_eventWildcards[$name][$class]);
        return true;
    }
    // plain event names
    if (isset(self::$_events[$name][$class])) {
        $removed = false;
        foreach (self::$_events[$name][$class] as $i => $event) {
            if ($event[0] === $handler) {
                unset(self::$_events[$name][$class][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            self::$_events[$name][$class] = array_values(self::$_events[$name][$class]);
            return true;
        }
    }
    // wildcard event names
    $removed = false;
    if (isset(self::$_eventWildcards[$name][$class])) {
        foreach (self::$_eventWildcards[$name][$class] as $i => $event) {
            if ($event[0] === $handler) {
                unset(self::$_eventWildcards[$name][$class][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]);
            // remove empty wildcards to save future redundant regex checks :
            if (empty(self::$_eventWildcards[$name][$class])) {
                unset(self::$_eventWildcards[$name][$class]);
                if (empty(self::$_eventWildcards[$name])) {
                    unset(self::$_eventWildcards[$name]);
                }
            }
        }
    }
    return $removed;
}

            
offAll() public static method (available since version 2.0.10)

Defined in: yii\base\Event::offAll()

Detaches all registered class-level event handlers.

See also:

public static void offAll ( )

                public static function offAll()
{
    self::$_events = [];
    self::$_eventWildcards = [];
}

            
on() public static method

Defined in: yii\base\Event::on()

Attaches an event handler to a class-level event.

When a class-level event is triggered, event handlers attached to that class and all parent classes will be invoked.

For example, the following code attaches an event handler to ActiveRecord's afterInsert event:

Event::on(ActiveRecord::class, ActiveRecord::EVENT_AFTER_INSERT, function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

The handler will be invoked for EVERY successful ActiveRecord insertion.

Since 2.0.14 you can specify either class name or event name as a wildcard pattern:

Event::on('app\models\db\*', '*Insert', function ($event) {
    Yii::trace(get_class($event->sender) . ' is inserted.');
});

For more details about how to declare an event handler, please refer to yii\base\Component::on().

See also off().

public static void on ( $class, $name, $handler, $data null, $append true )
$class string

The fully qualified class name to which the event handler needs to attach.

$name string

The event name.

$handler callable

The event handler.

$data mixed

The data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via yii\base\Event::$data.

$append boolean

Whether to append new event handler to the end of the existing handler list. If false, the new handler will be inserted at the beginning of the existing handler list.

                public static function on($class, $name, $handler, $data = null, $append = true)
{
    $class = ltrim($class, '\\');
    if (strpos($class, '*') !== false || strpos($name, '*') !== false) {
        if ($append || empty(self::$_eventWildcards[$name][$class])) {
            self::$_eventWildcards[$name][$class][] = [$handler, $data];
        } else {
            array_unshift(self::$_eventWildcards[$name][$class], [$handler, $data]);
        }
        return;
    }
    if ($append || empty(self::$_events[$name][$class])) {
        self::$_events[$name][$class][] = [$handler, $data];
    } else {
        array_unshift(self::$_events[$name][$class], [$handler, $data]);
    }
}

            
setVisible() public method

Setter method for the item visibility

public void setVisible ( $state )
$state integer

                public function setVisible($state)
{
    $this->item->isHidden = !$state;
}

            
trigger() public static method

Defined in: yii\base\Event::trigger()

Triggers a class-level event.

This method will cause invocation of event handlers that are attached to the named event for the specified class and all its parent classes.

public static void trigger ( $class, $name, $event null )
$class string|object

The object or the fully qualified class name specifying the class-level event.

$name string

The event name.

$event yii\base\Event|null

The event parameter. If not set, a default yii\base\Event object will be created.

                public static function trigger($class, $name, $event = null)
{
    $wildcardEventHandlers = [];
    foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
        if (!StringHelper::matchWildcard($nameWildcard, $name)) {
            continue;
        }
        $wildcardEventHandlers = array_merge($wildcardEventHandlers, $classHandlers);
    }
    if (empty(self::$_events[$name]) && empty($wildcardEventHandlers)) {
        return;
    }
    if ($event === null) {
        $event = new static();
    }
    $event->handled = false;
    $event->name = $name;
    if (is_object($class)) {
        if ($event->sender === null) {
            $event->sender = $class;
        }
        $class = get_class($class);
    } else {
        $class = ltrim($class, '\\');
    }
    $classes = array_merge(
        [$class],
        class_parents($class, true),
        class_implements($class, true)
    );
    foreach ($classes as $class) {
        $eventHandlers = [];
        foreach ($wildcardEventHandlers as $classWildcard => $handlers) {
            if (StringHelper::matchWildcard($classWildcard, $class, ['escape' => false])) {
                $eventHandlers = array_merge($eventHandlers, $handlers);
                unset($wildcardEventHandlers[$classWildcard]);
            }
        }
        if (!empty(self::$_events[$name][$class])) {
            $eventHandlers = array_merge($eventHandlers, self::$_events[$name][$class]);
        }
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            if ($event->handled) {
                return;
            }
        }
    }
}