Class luya\cms\Menu
Inheritance | luya\cms\Menu » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | ArrayAccess, luya\cms\menu\QueryOperatorFieldInterface, yii\base\Configurable |
Uses Traits | luya\traits\CacheableTrait |
Subclasses | luya\testsuite\components\DummyMenu |
Available since version | 1.0.0 |
Source Code | https://github.com/luyadev/luya-module-cms/blob/master/src/Menu.php |
Menu container component by language.
The {{luya\cms\Menu}} component returns an array with all menu items for a specific language, the class is designed to run in "singleton" mode. The {{luya\cms\Menu}} component provides also basic find methods like findAll, findOne to return specific data via the Query class. The menu components resolves also the current item based on the current request object.
Read more in the [[app-menu.md]] Guide.
 findAll() ¶
Example of getting all menu items for a specific where condition. The findAll() method does not provide the ability to add with statements, or chaning the language container as its a capsulated MenuQuery.
$itemsArray = Yii::$app->menu->findAll([self::FIELD_PARENTNAVID => 0, self::FIELD_PARENTNAVID => 1]);
findOne() ¶
Example of getting one menu item for a specific where condition. The findOne() method does not provide the ability to add with statements, or chaning the language container as its a capsulated MenuQuery.
$itemArray = Yii::$app->menu->findOne([self::FIELD_ID => 1]);
find() ¶
The find() methods is wrapper for creating a new \luya\cms\menu\Query().
$itemsArray = Yii::$app->menu->find()->where([self::FIELD_ID => 1])->lang('en')->all();
// is equal to:
$itemsArray = (new \luya\cms\menu\Query())->where([self::FIELD_ID => 1])->lang('en')->all();
 current ¶
One of the main goals of the menu component is to retrieve the current active loaded item, based on the current request object. You can always us getCurrent() or current to retrieve the current menu item.
$currentItem = Yii::$app->menu->getCurrent();
// is equal to:
$currentItem = Yii::$app->menu->current;
currentAppendix ¶
The getCurrentAppendix() method returns the parts from the url who are not related to the menu link, the appendix part is commonly a part for a module url rule used to switch between controlelrs and actions. The currentAppendix information will be resolved with the getCurrent() method.
 home ¶
The the home item for the current active language (defined by composition). As of Yii object inheritance you can use getHome() or home;
$homeItem = Yii::$app->menu->getHome();
// is equal to:
$homeItem = Yii::$app->menu->home;
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. | yii\base\Component |
$composition | luya\web\Composition | Get the composition. | luya\cms\Menu |
$current | luya\cms\menu\Item | Get the current active menu item. | luya\cms\Menu |
$currentAppendix | string | The url part not related for the current active link | luya\cms\Menu |
$currentUrlRule | array | Get the url rules for the current menu item. | luya\cms\Menu |
$encoding | boolean | Whether all menu item text values should be html encoded or not, since version 1.0.7.2 this is enabled by default in order to ensure administrators can not inject xss code into frontend. | luya\cms\Menu |
$home | luya\cms\menu\Item | Get the home menu item. | luya\cms\Menu |
$languages | array | An array where the key is the short_code and the value an array containing shortcode and id. | luya\cms\Menu |
$modulesMap | array | An array with all modules index by the id | luya\cms\Menu |
$redirectMap | array | An array where the key is the id and the value an array containing id, type and value. | luya\cms\Menu |
$request | luya\web\Request | Request object | luya\cms\Menu |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Class constructor to DI the request object. | luya\cms\Menu |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
buildItemLink() | Prepand the base url for the provided alias. | luya\cms\Menu |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
deleteHasCache() | Remove a value from the cache if caching is enabled. | luya\traits\CacheableTrait |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
encodeValue() | Determines and encodes the given value if enabled. | luya\cms\Menu |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
find() | Wrapper method for new \luya\cms\menu\Query() object. |
luya\cms\Menu |
findAll() | Wrapper method to get all menu items for the current language without hidden items for the specific where statement. | luya\cms\Menu |
findOne() | Wrapper method to get one menu item for current language without hidden items for the sepcific where statement. | luya\cms\Menu |
flushCache() | Flush all caching data for the menu for each language | luya\cms\Menu |
flushHasCache() | Deletes all values from cache. | luya\traits\CacheableTrait |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getComposition() | Get the composition component if not yet loaded. | luya\cms\Menu |
getCurrent() | Get the current menu item resolved by active language (from composition). | luya\cms\Menu |
getCurrentAppendix() | Returns the parts of the rules who do not belong to the current active menu item link. | luya\cms\Menu |
getCurrentUrlRule() | Get the url rules for the current menu item. | luya\cms\Menu |
getHasCache() | Get the caching data if caching is allowed and there is any data stored for this key. | luya\traits\CacheableTrait |
getHome() | Return the home site for the current resolved language resolved by {{luya\web\Composition}} component. | luya\cms\Menu |
getLanguage() | Get languag information for a specific language short code. | luya\cms\Menu |
getLanguageContainer() | Load and return the language container for the specific langShortCode, this method is used when using the ArrayAccess offsetGet() method. | luya\cms\Menu |
getLanguages() | Get an array with all available system languages based on the admin_lang table. | luya\cms\Menu |
getLevelContainer() | Get all items for a specific level. | luya\cms\Menu |
getLevelCurrent() | Get the current item for specified level/depth. Menus always start on level/depth 1. This works only in reversed order, to get the parent level from a child! | luya\cms\Menu |
getModulesMap() | Return all nav item modules to request data later in items | luya\cms\Menu |
getOrSetHasCache() | Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key. | luya\traits\CacheableTrait |
getRedirectMap() | Get an array containing all redirect items from the database table cms_nav_item_redirect. | luya\cms\Menu |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\base\BaseObject |
injectItem() | luya\cms\Menu | |
isCachable() | Check if the current configuration of the application and the property allows a caching of the language container data. | luya\traits\CacheableTrait |
off() | Detaches an existing event handler from this component. | yii\base\Component |
offsetExists() | ArrayAccess check if the offset key exists in the language array. | luya\cms\Menu |
offsetGet() | ArrayAccess get the current offset key if not yet loaded. | luya\cms\Menu |
offsetSet() | ArrayAccess offsetSet is not implemented in singleton component. | luya\cms\Menu |
offsetUnset() | ArrayAccess offsetSet is not implemented in singleton component. | luya\cms\Menu |
on() | Attaches an event handler to an event. | yii\base\Component |
setCurrent() | Ability to override the current item in order to make real preview urls (as when resolver does not work of changed urls). | luya\cms\Menu |
setCurrentUrlRule() | Set url rules for the current page item in order to retrieve at another point of the appliation when building language links. | luya\cms\Menu |
setHasCache() | Store cache data for a specific key if caching is enabled in this application. | luya\traits\CacheableTrait |
setLanguageContainer() | Setter method for the language container. | luya\cms\Menu |
trigger() | Triggers an event. | yii\base\Component |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
EVENT_AFTER_LOAD | 'eventAfterLoad' | luya\cms\Menu | |
EVENT_AFTER_RESOLVE_CURRENT | 'eventAfterResolveCurrent' | luya\cms\Menu | |
EVENT_ON_ITEM_FIND | 'eventOnItemFind' | luya\cms\Menu | |
ITEM_TYPE_MODULE | 2 | luya\cms\Menu | |
ITEM_TYPE_PAGE | 1 | luya\cms\Menu | |
ITEM_TYPE_REDIRECT | 3 | luya\cms\Menu |
Property Details
Get the current active menu item.
The url part not related for the current active link
Get the url rules for the current menu item.
Whether all menu item text values should be html encoded or not, since version 1.0.7.2 this is enabled by default in order to
ensure administrators can not inject xss code into frontend. If you where "abusing" the description or any other fields in order to use
html you can disable this feature with 'encoding' => false
when configure the menu component in your config.
An array where the key is the short_code and the value an array containing shortcode and id.
An array with all modules index by the id
An array where the key is the id and the value an array containing id, type and value.
Method Details
Defined in: yii\base\Component::__call()
Calls the named method which is not a class method.
This method will check if any attached behavior has the named method and will execute it if available.
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)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Defined in: yii\base\Component::__clone()
This method is called after the object is created by cloning an existing one.
It removes all behaviors because they are attached to the old object.
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
Class constructor to DI the request object.
public void __construct ( luya\web\Request $request, array $config = [] ) | ||
$request | luya\web\Request |
The request object resolved by DI. |
$config | array |
public function __construct(\luya\web\Request $request, array $config = [])
{
$this->request = $request;
parent::__construct($config);
}
Defined in: yii\base\Component::__get()
Returns the value of a component property.
This method will check in the following order and act accordingly:
- a property defined by a getter: return the getter result
- a property of a behavior: return the behavior property value
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $component->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string |
The property name |
return | mixed |
The property value or the value of a behavior's property |
---|---|---|
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)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (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);
}
Defined in: yii\base\Component::__isset()
Checks if a property is set, i.e. defined and not null.
This method will check in the following order and act accordingly:
- a property defined by a setter: return whether the property is set
- a property of a behavior: return whether the property is set
- return
false
for non existing properties
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($component->property)
.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name |
return | boolean |
Whether the named property is set |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
Defined in: yii\base\Component::__set()
Sets the value of a component property.
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value
- an event in the format of "on xyz": attach the handler to the event "xyz"
- a behavior in the format of "as xyz": attach the behavior named as "xyz"
- a property of a behavior: set the behavior property value
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $component->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)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\Component::__unset()
Sets a component property to be null.
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value to be null
- a property of a behavior: set the property value to be null
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($component->property)
.
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);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\Component::attachBehavior()
Attaches a behavior to this component.
This method will create the behavior object based on the given configuration. After that, the behavior object will be attached to this component by calling the yii\base\Behavior::attach() method.
See also detachBehavior().
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
The name of the behavior. |
$behavior | string|array|yii\base\Behavior |
The behavior configuration. This can be one of the following:
|
return | yii\base\Behavior |
The behavior object |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
Defined in: yii\base\Component::attachBehaviors()
Attaches a list of behaviors to the component.
Each behavior is indexed by its name and should be a yii\base\Behavior object, a string specifying the behavior class, or an configuration array for creating the behavior.
See also attachBehavior().
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
List of behaviors to be attached to the component |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
Defined in: yii\base\Component::behaviors()
Returns a list of behaviors that this component should behave as.
Child classes may override this method to specify the behaviors they want to behave as.
The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
Note that a behavior class must extend from yii\base\Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using getBehavior() or be detached using detachBehavior(). Anonymous behaviors can not be retrieved or detached.
Behaviors declared in this method will be attached to the component automatically (on demand).
public array behaviors ( ) | ||
return | array |
The behavior configurations. |
---|
public function behaviors()
{
return [];
}
Prepand the base url for the provided alias.
public string buildItemLink ( $alias, $langShortCode ) | ||
$alias | string | |
$langShortCode |
public function buildItemLink($alias, $langShortCode)
{
return Yii::$app->getUrlManager()->prependBaseUrl($this->composition->prependTo($alias, $this->composition->createRouteEnsure(['langShortCode' => $langShortCode])));
}
Defined in: yii\base\Component::canGetProperty()
Returns a value indicating whether a property can be read.
A property can be read 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); - an attached behavior has a readable property of the given name (when
$checkBehaviors
is true).
See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
$checkBehaviors | boolean |
Whether to treat behaviors' properties as properties of this component |
return | boolean |
Whether the property can be read |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
Defined in: yii\base\Component::canSetProperty()
Returns a value indicating whether a property can be set.
A property can be written 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); - an attached behavior has a writable property of the given name (when
$checkBehaviors
is true).
See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
$checkBehaviors | boolean |
Whether to treat behaviors' properties as properties of this component |
return | boolean |
Whether the property can be written |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
::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();
}
Defined in: luya\traits\CacheableTrait::deleteHasCache()
Remove a value from the cache if caching is enabled.
public boolean deleteHasCache ( $key ) | ||
$key | string|array |
The cache identifier |
return | boolean |
Whether delete of key has been success or not |
---|
public function deleteHasCache($key)
{
if ($this->isCachable()) {
return Yii::$app->cache->delete($key);
}
return false;
}
Defined in: yii\base\Component::detachBehavior()
Detaches a behavior from the component.
The behavior's yii\base\Behavior::detach() method will be invoked.
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
The behavior's name. |
return | yii\base\Behavior|null |
The detached behavior. Null if the behavior does not exist. |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
Defined in: yii\base\Component::detachBehaviors()
Detaches all behaviors from the component.
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
Determines and encodes the given value if enabled.
public string encodeValue ( $value ) | ||
$value | string |
The value to encode. |
return | string |
The encoded value if enabled. |
---|
public function encodeValue($value)
{
return $this->encoding ? Html::encode($value) : $value;
}
Defined in: yii\base\Component::ensureBehaviors()
Makes sure that the behaviors declared in behaviors() are attached to this component.
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
Wrapper method for new \luya\cms\menu\Query()
object.
public luya\cms\menu\Query find ( ) |
public function find()
{
return (new MenuQuery());
}
Wrapper method to get all menu items for the current language without hidden items for the specific where statement.
See also luya\cms\menu\Query::where().
public luya\cms\menu\QueryIterator findAll ( array $where, $preloadModels = false ) | ||
$where | array |
See {{\luya\cms\menu\Query::where()}} |
$preloadModels | boolean |
Whether to preload all models for the given menu Query. See {{luya\cms\menu\Query::preloadModels()}} |
public function findAll(array $where, $preloadModels = false)
{
return (new MenuQuery())->where($where)->preloadModels($preloadModels)->all();
}
Wrapper method to get one menu item for current language without hidden items for the sepcific where statement.
See also luya\cms\menu\Query::where().
public luya\cms\menu\Item findOne ( array $where ) | ||
$where | array |
See {{\luya\cms\menu\Query::where()}} |
public function findOne(array $where)
{
return (new MenuQuery())->where($where)->one();
}
Flush all caching data for the menu for each language
public void flushCache ( ) |
public function flushCache()
{
foreach ($this->getLanguages() as $lang) {
$this->deleteHasCache($this->_cachePrefix . $this->request->hostName . $lang['short_code']);
}
}
Defined in: luya\traits\CacheableTrait::flushHasCache()
Deletes all values from cache.
public boolean flushHasCache ( ) | ||
return | boolean |
Whether the flush operation was successful. |
---|
public function flushHasCache()
{
if ($this->isCachable()) {
return Yii::$app->cache->flush();
}
return false;
}
Defined in: yii\base\Component::getBehavior()
Returns the named behavior object.
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
The behavior name |
return | yii\base\Behavior|null |
The behavior object, or null if the behavior does not exist |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
Defined in: yii\base\Component::getBehaviors()
Returns all behaviors attached to this component.
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
List of behaviors attached to this component |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
Get the composition component if not yet loaded.
public luya\web\Composition getComposition ( ) | ||
return | luya\web\Composition |
Object with composition informationes. |
---|
public function getComposition()
{
if ($this->_composition === null) {
$this->_composition = Yii::$app->get('composition');
}
return $this->_composition;
}
Get the current menu item resolved by active language (from composition).
public luya\cms\menu\Item getCurrent ( ) | ||
return | luya\cms\menu\Item |
An item-object for the current active item. |
---|
public function getCurrent()
{
if ($this->_current === null) {
$this->_current = $this->resolveCurrent();
$this->trigger(self::EVENT_AFTER_RESOLVE_CURRENT);
}
return $this->_current;
}
Returns the parts of the rules who do not belong to the current active menu item link.
For instance the current menu link is /de/company/news
where this
page is declared as a module with urlRules, controllers and actions. If the url path is /de/company/news/detail/my-first-news
the
appendix would be detail/my-first-news
.
public string getCurrentAppendix ( ) | ||
return | string |
The url part not related for the current active link |
---|
public function getCurrentAppendix()
{
if ($this->_current === null) {
$this->_current = $this->resolveCurrent();
}
return $this->_currentAppendix;
}
Get the url rules for the current menu item.
public array getCurrentUrlRule ( ) |
public function getCurrentUrlRule()
{
return $this->_currentUrlRule;
}
Defined in: luya\traits\CacheableTrait::getHasCache()
Get the caching data if caching is allowed and there is any data stored for this key.
public mixed|boolean getHasCache ( $key ) | ||
$key | string|array |
The identifiere key, can be a string or an array which will be calculated. |
return | mixed|boolean |
Returns the data, if not found returns false. |
---|
public function getHasCache($key)
{
if ($this->isCachable()) {
$data = Yii::$app->cache->get($key);
$enumKey = (is_array($key)) ? implode(",", $key) : $key;
if ($data) {
Yii::debug("Cacheable trait key '$enumKey' successfully loaded from cache.", __METHOD__);
return $data;
}
Yii::debug("Cacheable trait key '$enumKey' has not been found in cache.", __METHOD__);
}
return false;
}
Return the home site for the current resolved language resolved by {{luya\web\Composition}} component.
public luya\cms\menu\Item getHome ( ) | ||
return | luya\cms\menu\Item |
An item-object for the home item for the current resolved language. |
---|
public function getHome()
{
return (new MenuQuery())->where([self::FIELD_ISHOME => 1])->with('hidden')->one();
}
Get languag information for a specific language short code.
public array|boolean getLanguage ( $shortCode ) | ||
$shortCode | string |
E.g. de or en |
return | array|boolean |
If the shortCode exists an array with informations as returned, otherwise false. |
---|
public function getLanguage($shortCode)
{
return (array_key_exists($shortCode, $this->getLanguages())) ? $this->getLanguages()[$shortCode] : false;
}
Load and return the language container for the specific langShortCode, this method is used when using the ArrayAccess offsetGet() method.
public array getLanguageContainer ( $langShortCode ) | ||
$langShortCode | string |
A language short code which have to access in $this->getLanguages(). |
return | array |
An array containing all items in theyr keys for the provided language short code. |
---|
public function getLanguageContainer($langShortCode)
{
if (!array_key_exists($langShortCode, $this->_languageContainer)) {
$this->_languageContainer[$langShortCode] = $this->loadWebsiteLanguageContainer($langShortCode);
$this->trigger(self::EVENT_AFTER_LOAD);
}
return $this->_languageContainer[$langShortCode];
}
Get an array with all available system languages based on the admin_lang table.
public array getLanguages ( ) | ||
return | array |
An array where the key is the short_code and the value an array containing shortcode and id. |
---|
public function getLanguages()
{
if ($this->_languages === null) {
$this->_languages = (new DbQuery())->select(['short_code', 'id'])->indexBy('short_code')->from('admin_lang')->all();
}
return $this->_languages;
}
Get all items for a specific level.
public array|luya\cms\menu\QueryIterator getLevelContainer ( $level, luya\cms\menu\Item $baseItem = null ) | ||
$level | integer |
Define the level you like to get the items from, the hirarchy starts by the level 1, which is the root line. |
$baseItem | luya\cms\menu\Item |
Provide an optional element which represents the base calculation item for Theme siblings/children calculation, this can be case when you have several contains do not want to use the "current" Item as base calucation, because getCurrent() could be in another container so it would get you all level container items for this container. |
return | array|luya\cms\menu\QueryIterator |
All siblings or children items, if not found an empty array will return. |
---|
public function getLevelContainer($level, Item $baseItem = null)
{
// define if the requested level is the root line (level 1) or not
$rootLine = ($level === 1) ? true : false;
// countdown the level (as we need the parent of the element to get the children
$level--;
// foreach counter
$i = 1;
if ($baseItem === null) {
$baseItem = $this->getCurrent();
}
// foreach
foreach ($baseItem->with('hidden')->getTeardown() as $item) {
// if its the root line an match level 1 get all siblings
if ($rootLine && $i == 1) {
return $item->without(['hidden'])->siblings;
} elseif ($i == $level) {
return $item->without(['hidden'])->children;
}
$i++;
}
// no no container found for the defined level
return [];
}
Get the current item for specified level/depth. Menus always start on level/depth 1. This works only in reversed order, to get the parent level from a child!
public luya\cms\menu\Item|boolean getLevelCurrent ( $level ) | ||
$level | integer |
Level menu starts with 1 |
return | luya\cms\menu\Item|boolean |
An item-object for the specific level current, false otherwise. |
---|
public function getLevelCurrent($level)
{
$i = 1;
foreach ($this->getCurrent()->with('hidden')->getTeardown() as $item) {
if ($i == $level) {
return $item;
}
++$i;
}
return false;
}
Return all nav item modules to request data later in items
public array getModulesMap ( ) | ||
return | array |
An array with all modules index by the id |
---|
public function getModulesMap()
{
if ($this->_modulesMap === null) {
$this->_modulesMap = NavItemModule::find()->select(['module_name', 'id'])->indexBy('id')->asArray()->all();
}
return $this->_modulesMap;
}
Defined in: luya\traits\CacheableTrait::getOrSetHasCache()
Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key.
Usage example:
use CacheableTrait;
public function getTopProducts($count = 10)
{
return $this->getOrSetHasCache(['top-n-products', 'n' => $count], function ($cache) use ($count) {
return Products::find()->mostPopular()->limit(10)->all();
}, 1000);
}
public mixed getOrSetHasCache ( $key, Closure $closure, $duration = null, $dependency = null ) | ||
$key | mixed |
A key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key. |
$closure | Closure |
The closure that will be used to generate a value to be cached.
In case $closure returns |
$duration | integer |
Default duration in seconds before the cache will expire. If not set, defaultDuration value will be used. 0 means never expire. |
$dependency | yii\caching\Dependency |
Dependency of the cached item. If the dependency changes,
the corresponding value in the cache will be invalidated when it is fetched via get().
This parameter is ignored if serializer is |
return | mixed |
Result of $closure execution |
---|
public function getOrSetHasCache($key, \Closure $closure, $duration = null, $dependency = null)
{
if (($value = $this->getHasCache($key)) !== false) {
return $value;
}
$value = call_user_func($closure, $this);
$this->setHasCache($key, $value, $dependency, $duration);
return $value;
}
Get an array containing all redirect items from the database table cms_nav_item_redirect.
public array getRedirectMap ( ) | ||
return | array |
An array where the key is the id and the value an array containing id, type and value. |
---|
public function getRedirectMap()
{
if ($this->_redirectMap === null) {
$this->_redirectMap = (new DbQuery())->select(['id', 'type', 'value', 'target'])->from('cms_nav_item_redirect')->indexBy('id')->all();
}
return $this->_redirectMap;
}
Defined in: yii\base\Component::hasEventHandlers()
Returns a value indicating whether there is any handler attached to the named event.
public boolean hasEventHandlers ( $name ) | ||
$name | string |
The event name |
return | boolean |
Whether there is any handler attached to the event. |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
Defined in: yii\base\Component::hasMethod()
Returns a value indicating whether a method is defined.
A method is defined if:
- the class has a method with the specified name
- an attached behavior has a method with the given name (when
$checkBehaviors
is true).
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
The property name |
$checkBehaviors | boolean |
Whether to treat behaviors' methods as methods of this component |
return | boolean |
Whether the method is defined |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
Defined in: yii\base\Component::hasProperty()
Returns a value indicating whether a property is defined for this component.
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); - an attached behavior has a property of the given name (when
$checkBehaviors
is true).
See also:
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
$checkBehaviors | boolean |
Whether to treat behaviors' properties as properties of this component |
return | boolean |
Whether the property is defined |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
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()
{
}
public void injectItem ( luya\cms\menu\InjectItemInterface $item ) | ||
$item | luya\cms\menu\InjectItemInterface |
public function injectItem(InjectItemInterface $item)
{
$this->_languageContainer[$item->getLang()][$item->getId()] = $item->toArray();
}
Defined in: luya\traits\CacheableTrait::isCachable()
Check if the current configuration of the application and the property allows a caching of the language container data.
public boolean isCachable ( ) |
public function isCachable()
{
if ($this->_cachable === null) {
$this->_cachable = Yii::$app->has('cache') ? true : false;
}
return $this->_cachable;
}
Defined in: yii\base\Component::off()
Detaches an existing event handler from this component.
This method is the opposite of on().
Note: in case wildcard pattern is passed for 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 boolean off ( $name, $handler = null ) | ||
$name | string |
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 |
If a handler is found and detached |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
ArrayAccess check if the offset key exists in the language array.
public boolean offsetExists ( $offset ) | ||
$offset | string |
Language short code the verify |
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return ($this->getLanguage($offset)) ? true : false;
}
ArrayAccess get the current offset key if not yet loaded.
public array|mixed offsetGet ( $offset ) | ||
$offset | string |
Language short code to get |
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->getLanguageContainer($offset);
}
ArrayAccess offsetSet is not implemented in singleton component.
public void offsetSet ( $offset, $value ) | ||
$offset | string | |
$value | string | |
throws | luya\cms\Exception |
Always throws exception as this method is not implemented. |
---|
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
throw new Exception('The set method is disabled for this component.');
}
ArrayAccess offsetSet is not implemented in singleton component.
public void offsetUnset ( $offset ) | ||
$offset | string | |
throws | luya\cms\Exception |
Always throws exception as this method is not implemented. |
---|
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
throw new Exception('The unset method is disabled for this component.');
}
Defined in: yii\base\Component::on()
Attaches an event handler to an event.
The event handler must be a valid PHP callback. The following are some examples:
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
The event handler must be defined with the following signature,
function ($event)
where $event
is an yii\base\Event object which includes parameters associated with the event.
Since 2.0.14 you can specify event name as a wildcard pattern:
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
See also off().
public void on ( $name, $handler, $data = null, $append = true ) | ||
$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 function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
Ability to override the current item in order to make real preview urls (as when resolver does not work of changed urls).
public void setCurrent ( luya\cms\menu\Item $item ) | ||
$item | luya\cms\menu\Item |
The current item object |
public function setCurrent(Item $item)
{
$this->_current = $item;
}
Set url rules for the current page item in order to retrieve at another point of the appliation when building language links.
public void setCurrentUrlRule ( array $rule ) | ||
$rule | array |
public function setCurrentUrlRule(array $rule)
{
$this->_currentUrlRule = $rule;
}
Defined in: luya\traits\CacheableTrait::setHasCache()
Store cache data for a specific key if caching is enabled in this application.
public boolean setHasCache ( $key, $value, $dependency = null, $cacheExpiration = null ) | ||
$key | string|array |
The identifier key or a array to store complex keys |
$value | mixed |
The value to store in the cache component. |
$dependency | yii\caching\Dependency|array |
Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if $serializer is false. You can also define an array with defintion which will generate the Object instead of object is provided. |
$cacheExpiration |
Integer The time in seconds before the cache data expires, 0 means never expire. |
|
return | boolean |
Whether set has been success or not |
---|
public function setHasCache($key, $value, $dependency = null, $cacheExpiration = null)
{
if ($this->isCachable()) {
if (is_array($dependency)) {
$dependency = Yii::createObject($dependency);
}
return Yii::$app->cache->set($key, $value, is_null($cacheExpiration) ? $this->_cacheExpiration : $cacheExpiration, $dependency);
}
return false;
}
Setter method for the language container.
This is maily used when working with unit test.
Example Data array:
[
43 => [
'id' => '43',
'nav_id' => '1',
'lang' => 'fr',
'link' => '/public_html/fr/home',
'title' => 'Homepage',
'alias' => 'home',
'description' => '',
'keywords' => NULL,
'create_user_id' => '4',
'update_user_id' => '4',
'timestamp_create' => '1457091369',
'timestamp_update' => '1483367249',
'is_home' => '1',
'parent_nav_id' => '0',
'sort_index' => '1',
'is_hidden' => '1',
'type' => '1',
'nav_item_type_id' => '42',
'redirect' => false,
'module_name' => false,
'container' => 'default',
'depth' => 1,
'image_id' => 0,
'is_url_strict_parsing_disabled' => 1,
],
]
public void setLanguageContainer ( $langShortCode, array $data ) | ||
$langShortCode | string | |
$data | array |
An array with items based on the database |
public function setLanguageContainer($langShortCode, array $data)
{
$this->_languageContainer[$langShortCode] = $data;
}
Defined in: yii\base\Component::trigger()
Triggers an event.
This method represents the happening of an event. It invokes all attached handlers for the event including class-level handlers.
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
The event name |
$event | yii\base\Event|null |
The event instance. If not set, a default yii\base\Event object will be created. |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}