Class luya\cms\menu\InjectItem

Inheritanceluya\cms\menu\InjectItem » yii\base\BaseObject
Implementsluya\cms\menu\InjectItemInterface, yii\base\Configurable
Available since version1.0.0

An item inject gives a module the possibility to add items into the menu Container.

The most important propertie of the injectItem clas is the childOf definition, this is where you have to define who is the parent nav_item.id.

An item inject contain be done during the eventAfterLoad event to attach at the right initializer moment of the item, but could be done any time. To inject an item use the injectItem method on the menu Container like below:

Yii::$app->menu->injectItem(new InjectItem([
    'childOf' => 123,
    'title' => 'This is the inject title',
    'alias' => 'this-is-the-inject-alias',
]));

Instead of using the childOf property you can directly set an menu item:

Yii::$app->menu->injectItem(new InjectItem([
    'item' => Yii::$app->menu->current,
    'title' => 'This is the inject title',
    'alias' => 'this-is-the-inject-alias',
]));

You can also use the chain method to create a new inject item:

$item = (new InjectItem())->setTitle('My Title')->setAlias('my Alias')->setItem(Yii::$app->menu->current);

To attach the item at right moment you can bootstrap your module and use the eventAfterLoad event of the menu component:

Yii::$app->menu->on(\luya\cms\Menu::EVENT_AFTER_RESOLVE_CURRENT, function($event) {

    $newItem = new InjectItem([
        'childOf' => 123,
        'title' => 'Inject Title',
        'alias' => 'inject-title',
    ]);

    $event->sender->injectItem($newItem);
});

Inside a configuration file this could be done as:

'menu' => [
    'class' => 'luya\cms\Menu',
    'on eventAfterLoad' => function($event) {
        // ...
        $event->sender->injectItem($newItem);
    }
]

Public Properties

Hide inherited properties

Property Type Description Defined By
$alias The alias path. luya\cms\menu\InjectItem
$childOf integer The child of id in order read data from this parent item. luya\cms\menu\InjectItem
$container integer luya\cms\menu\InjectItem
$createUserId integer The user id who created this page. luya\cms\menu\InjectItem
$dept \luya\cms\menu\number luya\cms\menu\InjectItem
$description Alternative page descriptions. luya\cms\menu\InjectItem
$id integer|string The id (navItemId) for this inject item. luya\cms\menu\InjectItem
$isHidden boolean Whether this page is hidden or not. luya\cms\menu\InjectItem
$isHome \luya\cms\menu\number luya\cms\menu\InjectItem
$item \luya\cms\menu\Item The resolved Item. luya\cms\menu\InjectItem
$lang The language container name. luya\cms\menu\InjectItem
$navId integer|string The navId for this inject item. luya\cms\menu\InjectItem
$parentNavId integer luya\cms\menu\InjectItem
$redirect \luya\cms\menu\number luya\cms\menu\InjectItem
$sortIndex integer The number which is used to sort the injected item. luya\cms\menu\InjectItem
$timestampCreate \luya\cms\menu\number luya\cms\menu\InjectItem
$timestampUpdate \luya\cms\menu\number luya\cms\menu\InjectItem
$title The navigation menu title. luya\cms\menu\InjectItem
$type \luya\cms\menu\number luya\cms\menu\InjectItem
$updateUserId \luya\cms\menu\integerThe User id who updated this page as last person. luya\cms\menu\InjectItem

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
getAlias() Getter method for the alias. luya\cms\menu\InjectItem
getChildOf() Getter method for the childOf property. luya\cms\menu\InjectItem
getContainer() Getter method for the container from the child of item. luya\cms\menu\InjectItem
getDept() Returns the depth number based on the alias paths. luya\cms\menu\InjectItem
getDescription() Getter method for the menu page description. luya\cms\menu\InjectItem
getId() Getter method for the unique id. luya\cms\menu\InjectItem
getIsHome() Getter method for the isHhome item, which is false by default. luya\cms\menu\InjectItem
getItem() Returns the evalutead menu item whether from the childOf property or set from the setter method. luya\cms\menu\InjectItem
getLang() Returns the language from the childOf item. luya\cms\menu\InjectItem
getLink() Getter method for the menu link. luya\cms\menu\InjectItem
getNavId() Getter method for the navId. luya\cms\menu\InjectItem
getParentNavId() Getter method for the parent nav id from the childOf item. luya\cms\menu\InjectItem
getRedirect() Getter method for the redirect content if its a redirect page, by default 0. luya\cms\menu\InjectItem
getTimestampCreate() Getter method for the create item timestamp. luya\cms\menu\InjectItem
getTimestampUpdate() Getter method for the update item timestamp. luya\cms\menu\InjectItem
getTitle() Getter method for the menu title. luya\cms\menu\InjectItem
getType() Getter method for the type which is by default a page. luya\cms\menu\InjectItem
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
setAlias() Setter method for the item alias. luya\cms\menu\InjectItem
setChildOf() Setter method for childOf property. luya\cms\menu\InjectItem
setDescription() Setter method for menu page description. luya\cms\menu\InjectItem
setId() Setter method for the id (unique id). luya\cms\menu\InjectItem
setItem() Setter method for the item property. luya\cms\menu\InjectItem
setLang() Setter method for language container. luya\cms\menu\InjectItem
setLink() Setter method fro the link. luya\cms\menu\InjectItem
setNavId() Setter method for the navId. luya\cms\menu\InjectItem
setTitle() Setter method for the menu title. luya\cms\menu\InjectItem
toArray() Parse the injected item to an array. luya\cms\menu\InjectItem

Property Details

Hide inherited properties

$alias public property

The alias path.

public $alias null
$childOf public property

integer The child of id in order read data from this parent item.

public $childOf null
$container public read-only property
$createUserId public property

The user id who created this page.

$dept public read-only property
public \luya\cms\menu\number getDept ( )
$description public property

Alternative page descriptions.

public $description null
$id public property

integer|string The id (navItemId) for this inject item.

public $id null
$isHidden public property

Whether this page is hidden or not.

public boolean $isHidden false
$isHome public read-only property
public \luya\cms\menu\number getIsHome ( )
$item public property

\luya\cms\menu\Item The resolved Item.

public $item null
$lang public property

The language container name.

public $lang null
$navId public property

integer|string The navId for this inject item.

public $navId null
$parentNavId public read-only property
$redirect public read-only property
public \luya\cms\menu\number getRedirect ( )
$sortIndex public property

The number which is used to sort the injected item. Lower is at the top.

public integer $sortIndex 0
$timestampCreate public read-only property
public \luya\cms\menu\number getTimestampCreate ( )
$timestampUpdate public read-only property
public \luya\cms\menu\number getTimestampUpdate ( )
$title public property

The navigation menu title.

public $title null
$type public read-only property
public \luya\cms\menu\number getType ( )
$updateUserId public property

User id who updated this page as last person.

public \luya\cms\menu\integerThe $updateUserId 0

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();
}

            
getAlias() public method

Getter method for the alias.

public string getAlias ( )
return string

The alias with the parent childOf alias prefixed.

throws luya\cms\Exception

                public function getAlias()
{
    if ($this->_alias === null) {
        throw new Exception('The $alias property can not be null and must be set.');
    }
    return $this->item->alias . '/' . $this->_alias;
}

            
getChildOf() public method

Getter method for the childOf property.

public \luya\cms\menu\number getChildOf ( )
throws luya\cms\Exception

                public function getChildOf()
{
    if ($this->_childOf === null) {
        throw new Exception("In order to inject an item, you have to set the `childOf` property.");
    }
    return $this->_childOf;
}

            
getContainer() public method

Getter method for the container from the child of item.

public integer getContainer ( )

                public function getContainer()
{
    return $this->item->container;
}

            
getDept() public method

Returns the depth number based on the alias paths.

public \luya\cms\menu\number getDept ( )

                public function getDept()
{
    return count(explode('/', $this->alias));
}

            
getDescription() public method

Getter method for the menu page description.

public string getDescription ( )
return string

The menu item description.

                public function getDescription()
{
    return $this->_description;
}

            
getId() public method

Getter method for the unique id.

public string|integer getId ( )

                public function getId()
{
    if ($this->_id === null) {
        $this->_id = random_int(10000, 1_000_000);
    }
    return $this->_id;
}

            
getIsHome() public method

Getter method for the isHhome item, which is false by default.

public \luya\cms\menu\number getIsHome ( )

                public function getIsHome()
{
    return (int) false;
}

            
getItem() public method

Returns the evalutead menu item whether from the childOf property or set from the setter method.

public luya\cms\menu\Item getItem ( )
throws luya\cms\Exception

                public function getItem()
{
    if ($this->_item === null) {
        $this->_item = Yii::$app->menu->find()->where(['id' => $this->childOf])->with('hidden')->one();
        if (!$this->_item) {
            throw new Exception("Unable to find item with id " . $this->childOf);
        }
    }
    return $this->_item;
}

            
getLang() public method

Returns the language from the childOf item.

public string getLang ( )

                public function getLang()
{
    if ($this->_lang === null) {
        $this->_lang = $this->item->lang;
    }
    return $this->_lang;
}

            
getLink() public method

Getter method for the menu link.

public string getLink ( )
return string

The built link.

getNavId() public method

Getter method for the navId.

public string|\luya\cms\menu\unknown getNavId ( )

                public function getNavId()
{
    if ($this->_navId === null) {
        $this->_navId = random_int(10000, 1_000_000);
    }
    return $this->_navId;
}

            
getParentNavId() public method

Getter method for the parent nav id from the childOf item.

public integer getParentNavId ( )

                public function getParentNavId()
{
    return $this->item->navId;
}

            
getRedirect() public method

Getter method for the redirect content if its a redirect page, by default 0.

public \luya\cms\menu\number getRedirect ( )

                public function getRedirect()
{
    return 0;
}

            
getTimestampCreate() public method

Getter method for the create item timestamp.

public \luya\cms\menu\number getTimestampCreate ( )

                public function getTimestampCreate()
{
    return time();
}

            
getTimestampUpdate() public method

Getter method for the update item timestamp.

public \luya\cms\menu\number getTimestampUpdate ( )

                public function getTimestampUpdate()
{
    return time();
}

            
getTitle() public method

Getter method for the menu title.

public string getTitle ( )
throws luya\cms\Exception

                public function getTitle()
{
    if ($this->_title === null) {
        throw new Exception('The $title property can not be null and must be set.');
    }
    return $this->_title;
}

            
getType() public method

Getter method for the type which is by default a page.

public \luya\cms\menu\number getType ( )

                public function getType()
{
    return Menu::ITEM_TYPE_PAGE;
}

            
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()
{
}

            
setAlias() public method

Setter method for the item alias.

public luya\cms\menu\InjectItem setAlias ( $alias )
$alias string

A slugable alias string will be parsed by the inflector::slug method.

                public function setAlias($alias)
{
    $this->_alias = Inflector::slug($alias);
    return $this;
}

            
setChildOf() public method

Setter method for childOf property.

public luya\cms\menu\InjectItem setChildOf ( $id )
$id integer

                public function setChildOf($id)
{
    $this->_childOf = (int) $id;
    return $this;
}

            
setDescription() public method

Setter method for menu page description.

public luya\cms\menu\InjectItem setDescription ( $description )
$description string

Description for the menu item.

                public function setDescription($description)
{
    $this->_description = $description;
    return $this;
}

            
setId() public method

Setter method for the id (unique id).

public void setId ( $id )
$id integer

                public function setId($id)
{
    $this->_id = $id;
}

            
setItem() public method

Setter method for the item property.

public luya\cms\menu\InjectItem setItem ( luya\cms\menu\Item $item )
$item luya\cms\menu\Item

                public function setItem(Item $item)
{
    $this->childOf = $item->id;
    $this->_item = $item;
    return $this;
}

            
setLang() public method

Setter method for language container.

public void setLang ( $lang )
$lang string

The language short code for the given item, if nothing set the item will be resolved due to useage of the parent item from $this->item.

                public function setLang($lang)
{
    $this->_lang = $lang;
}

            
setLink() public method

Setter method fro the link.

public luya\cms\menu\InjectItem setLink ( $url )
$url string

setNavId() public method

Setter method for the navId.

public void setNavId ( $navId )
$navId \luya\cms\menu\unknown

                public function setNavId($navId)
{
    $this->_navId = $navId;
}

            
setTitle() public method

Setter method for the menu title.

public luya\cms\menu\InjectItem setTitle ( $title )
$title string

The menu item title.

                public function setTitle($title)
{
    // if the alias is empty use the title for the alias.
    if ($this->_alias === null) {
        $this->setAlias($title);
    }
    $this->_title = $title;
    return $this;
}

            
toArray() public method

Parse the injected item to an array.

public array toArray ( )

                public function toArray()
{
    return [
        'id' => $this->getId(),
        'nav_id' => $this->getNavId(),
        'lang' => $this->getLang(),
        'link' => $this->getLink(),
        'title' => Yii::$app->menu->encodeValue($this->title),
        'title_tag' => Yii::$app->menu->encodeValue($this->title),
        'alias' => $this->getAlias(),
        'description' => Yii::$app->menu->encodeValue($this->description),
        'keywords' => null,
        'create_user_id' => $this->createUserId,
        'update_user_id' => $this->updateUserId,
        'timestamp_create' => $this->getTimestampCreate(),
        'timestamp_update' => $this->getTimestampUpdate(),
        'is_home' => $this->getIsHome(),
        'parent_nav_id' => $this->getParentNavId(),
        'sort_index' => $this->sortIndex,
        'is_hidden' => (bool) $this->isHidden,
        'type' => $this->getType(),
        'redirect' => $this->getRedirect(),
        'container' => $this->getContainer(),
        'depth' => $this->getDept(),
    ];
}