Class TreeHelper
Helper to generate tree representations of MPTT or recursively nested data.
Property Summary
-
$config protected
array<string, mixed>Runtime config
-
$configInitialized protected
boolWhether the config property has already been configured with defaults
-
$currentConfig protected
array -
$defaultConfig protected
array<string, mixed>Default settings
-
$helperInstances protected
array<string, \Cake\View\Helper<Cake\View\View>>Loaded helper instances.
-
$helpers protected
array<int|string, string|array<string, mixed>>List of helpers used by this helper
-
$itemAttributes protected
array -
$typeAttributes protected
array -
$typeAttributesNext protected
array
Method Summary
-
__construct() public
Default Constructor
-
__debugInfo() public
Returns an array that can be used to describe the internal state of this object.
-
__get() public
Lazy loads helpers.
-
addClass() public
Adds the given class to the element options
-
addItemAttribute() public
AddItemAttribute function
Called to modify the attributes of the next
- to be processed Note that the content of a 'node' is processed before generating its wrapping
- tag
- to be processed Note that the content of a 'node' is processed before generating its wrapping
-
addTypeAttribute() public
AddTypeAttribute function
Called to modify the attributes of the next
to be processed Note that the content of a 'node' is processed before generating its wrapping tag (if appropriate) An 'interesting' case is that of a first child with children. To generate the output - (1)
- XYZ (3)
- (2)
- ABC... ...
- XYZ (3)
-
attributes() protected
Attributes function.
Logic to apply styles to tags.
-
calculateVisibility() protected
Calculate which unrelated nodes are hidden when
hideUnrelatedis enabled.This leaves the nodes untouched, the result is only used to skip nodes while rendering. It is keyed like
$nodesand, for threaded data, nested throughchildren. -
configDelete() protected
Deletes a single config key.
-
configRead() protected
Reads a config key.
-
configShallow() public
Merge provided config with existing config. Unlike
config()which does a recursive merge for nested keys, this method does a simple merge.Setting a specific value:
$this->configShallow('key', $value);Setting a nested value:
$this->configShallow('some.nested.key', $value);Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']); -
configWrite() protected
Writes a config key.
-
confirm() protected
Returns a string to be used as onclick handler for confirm dialogs.
-
deleteConfig() public
Deletes a config key.
-
flatParents() protected
Find the parent (
-1for roots) and level of each row of a flat MPTT result. -
flatVisibility() protected
-
generate() public
Tree generation method.
Accepts the results of find('all', fields: ['lft', 'rght', 'whatever'], order: 'lft ASC']; children(); // if the table has Tree behavior attached or find('threaded'); and generates a tree structure of the data.
Settings (2nd parameter): 'alias' => the array key to output for a simple ul (not used if element or callback is specified) 'type' => type of output defaults to UL 'itemType => type of item output default to LI 'id' => id for top level 'type' 'class' => class for top level 'type' 'element' => path to an element to render to get node contents. 'callback' => callback to use to get node contents. e.g. array(&$anObject, 'methodName') or 'floatingMethod' 'autoPath' => [$left, $right, $classToAdd = 'active'] if set any item in the path will have the class $classToAdd added. MPTT only. You can also just pass the current entity and it will extract lft and rght properties based on the config. 'hideUnrelated' => if unrelated (not children, not siblings) should be hidden, needs 'treePath', true/false, an integer (number of levels that are always shown) or a callable that marks nodes itself. Hidden nodes are skipped automatically, the nodes passed to callbacks are never modified. 'treePath' => list of node keys from the root to the current node. Inserted into callback/element and used to find the branch that stays open when 'hideUnrelated' is enabled. 'nodeKey' => name of the field identifying a node, matched against 'treePath'. Defaults to
id. 'level' => name of the field holding the depth of a node, same as the TreeBehaviorlevelconfig. If set, the depth of flat rows is read from it, relative to the first row, instead of being derived from the nesting. MPTT only. 'left' => name of the 'lft' field if not lft. only applies to MPTT data 'right' => name of the 'rght' field if not rght. only applies to MPTT data 'depth' => used internally when running recursively, can be used to override the depth in either mode. 'maxDepth' => used to control the depth upto which to generate tree 'firstChild' => used internally when running recursively. 'splitDepth' => if multiple "parallel" types are required, instead of one big type, nominate the depth to do so here example: useful if you have 30 items to display, and you'd prefer they appeared in the source as 3 lists of 10 to be able to style/float them. 'splitCount' => the number of "parallel" types. defaults to null (disabled) set the splitCount, and optionally set the splitDepth to get parallel lists. Each list of siblings is split on its own. -
generateSubTree() protected
-
getConfig() public
Returns the config.
Usage
Reading the whole config:
$this->getConfig();Reading a specific value:
$this->getConfig('key');Reading a nested value:
$this->getConfig('some.nested.key');Reading with default value:
$this->getConfig('some-key', 'default-value'); -
getConfigOrFail() public
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
-
getView() public
Get the view instance this helper is bound to.
-
implementedEvents() public
Get the View callbacks this helper is interested in.
By defining one of the callback methods a helper is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want helpers to listen to non-standard events.
-
initialize() public
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
-
isFlat() protected
Whether the nodes are a flat MPTT result instead of nested
children. -
setConfig() public
Sets the config.
Usage
Setting a specific value:
$this->setConfig('key', $value);Setting a nested value:
$this->setConfig('some.nested.key', $value);Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']); -
threadedVisibility() protected
Method Detail
__construct() ¶ public
__construct(TView $View, array<string, mixed> $config = [])
Default Constructor
Parameters
-
TView$View The View this helper is being attached to.
-
array<string, mixed>$config optional Configuration settings for the helper.
__debugInfo() ¶ public
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
Returns
array<string, mixed>__get() ¶ public
__get(string $name): Cake\View\Helper<Cake\View\View>|null
Lazy loads helpers.
Parameters
-
string$name Name of the property being accessed.
Returns
Cake\View\Helper<Cake\View\View>|nullHelper instance if helper with provided name exists
addClass() ¶ public
addClass(array<string, mixed> $options, string $class, string $key = 'class'): array<string, mixed>
Adds the given class to the element options
Parameters
-
array<string, mixed>$options Array options/attributes to add a class to
-
string$class The class name being added.
-
string$key optional the key to use for class. Defaults to
'class'.
Returns
array<string, mixed>Array of options with $key set.
addItemAttribute() ¶ public
addItemAttribute(string $id = '', string $key = '', mixed $value = null): void
AddItemAttribute function
Called to modify the attributes of the next
Parameters
-
string$id optional Id
-
string$key optional Key
-
mixed$value optional Value
Returns
voidaddTypeAttribute() ¶ public
addTypeAttribute(string $id = '', string $key = '', mixed|null $value = null, string $previousOrNext = 'next'): void
AddTypeAttribute function
Called to modify the attributes of the next
- (1)
- XYZ (3)
- (2)
- ABC... ...
Parameters
-
string$id optional ID
-
string$key optional Key
-
mixed|null$value optional Value
-
string$previousOrNext optional Previous or next
Returns
void
attributes() ¶ protected
attributes(string $rType, array $elementData = [], bool $clear = true, bool $isItem = false): string
Attributes function.
Logic to apply styles to tags.
Parameters
-
string$rType rType
-
array$elementData optional Element data
-
bool$clear optional Clear
-
bool$isItem optional is this an item or type call
Returns
stringcalculateVisibility() ¶ protected
calculateVisibility(array $nodes, bool $flat): array<int, array{show: bool, hide: bool, children?: array}>
Calculate which unrelated nodes are hidden when hideUnrelated is enabled.
This leaves the nodes untouched, the result is only used to skip nodes while rendering.
It is keyed like $nodes and, for threaded data, nested through children.
Parameters
-
array$nodes Nodes
-
bool$flat Whether the nodes are a flat MPTT result
Returns
array<int, array{show: bool, hide: bool, children?: array}>configDelete() ¶ protected
configDelete(string $key): void
Deletes a single config key.
Parameters
-
string$key Key to delete.
Returns
voidThrows
Cake\Core\Exception\CakeExceptionif attempting to clobber existing config
configRead() ¶ protected
configRead(string|null $key): $key is null ? array : mixed
Reads a config key.
Parameters
-
string|null$key Key to read.
Returns
$key is null ? array : mixedconfigShallow() ¶ public
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config() which does
a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string$key The key to set, or a complete array of configs.
-
mixed|null$value optional The value to set.
Returns
$thisconfigWrite() ¶ protected
configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
Parameters
-
array<string, mixed>|string$key Key to write to.
-
mixed$value Value to write.
-
string|bool$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
voidThrows
Cake\Core\Exception\CakeExceptionif attempting to clobber existing config
confirm() ¶ protected
confirm(string $okCode, string $cancelCode): string
Returns a string to be used as onclick handler for confirm dialogs.
Parameters
-
string$okCode Code to be executed after user chose 'OK'
-
string$cancelCode Code to be executed after user chose 'Cancel'
Returns
string"onclick" JS code
deleteConfig() ¶ public
deleteConfig(string $key): $this
Deletes a config key.
Parameters
-
string$key Key to delete. It can be a dot separated string to delete nested keys.
Returns
$thisflatParents() ¶ protected
flatParents(array $nodes): array{parents: array<int, int>, levels: array<int, int>}
Find the parent (-1 for roots) and level of each row of a flat MPTT result.
Parameters
-
array$nodes Nodes
Returns
array{parents: array<int, int>, levels: array<int, int>}flatVisibility() ¶ protected
flatVisibility(array $nodes): array<int, array{show: bool, hide: bool}>
Parameters
-
array$nodes Nodes
Returns
array<int, array{show: bool, hide: bool}>generate() ¶ public
generate(iterable $data, array<string, mixed> $config = []): string
Tree generation method.
Accepts the results of find('all', fields: ['lft', 'rght', 'whatever'], order: 'lft ASC']; children(); // if the table has Tree behavior attached or find('threaded'); and generates a tree structure of the data.
Settings (2nd parameter):
'alias' => the array key to output for a simple ul (not used if element or callback is specified)
'type' => type of output defaults to UL
'itemType => type of item output default to LI
'id' => id for top level 'type'
'class' => class for top level 'type'
'element' => path to an element to render to get node contents.
'callback' => callback to use to get node contents. e.g. array(&$anObject, 'methodName') or 'floatingMethod'
'autoPath' => [$left, $right, $classToAdd = 'active'] if set any item in the path will have the class $classToAdd added. MPTT only.
You can also just pass the current entity and it will extract lft and rght properties based on the config.
'hideUnrelated' => if unrelated (not children, not siblings) should be hidden, needs 'treePath', true/false,
an integer (number of levels that are always shown) or a callable that marks nodes itself.
Hidden nodes are skipped automatically, the nodes passed to callbacks are never modified.
'treePath' => list of node keys from the root to the current node. Inserted into callback/element and
used to find the branch that stays open when 'hideUnrelated' is enabled.
'nodeKey' => name of the field identifying a node, matched against 'treePath'. Defaults to id.
'level' => name of the field holding the depth of a node, same as the TreeBehavior level config.
If set, the depth of flat rows is read from it, relative to the first row, instead of being derived
from the nesting. MPTT only.
'left' => name of the 'lft' field if not lft. only applies to MPTT data
'right' => name of the 'rght' field if not rght. only applies to MPTT data
'depth' => used internally when running recursively, can be used to override the depth in either mode.
'maxDepth' => used to control the depth upto which to generate tree
'firstChild' => used internally when running recursively.
'splitDepth' => if multiple "parallel" types are required, instead of one big type, nominate the depth to do so here
example: useful if you have 30 items to display, and you'd prefer they appeared in the source as 3 lists of 10 to be able to
style/float them.
'splitCount' => the number of "parallel" types. defaults to null (disabled) set the splitCount,
and optionally set the splitDepth to get parallel lists. Each list of siblings is split on its own.
Parameters
-
iterable$data Data to loop over
-
array<string, mixed>$config optional Config
Returns
stringHTML representation of the passed data
Throws
ExceptiongenerateSubTree() ¶ protected
generateSubTree(iterable $data, array<string, mixed> $config, Cake\Datasource\EntityInterface|iterable|array|null $parent = null, array<int, array<string, mixed>> $visibility = []): string
Parameters
-
iterable$data -
array<string, mixed>$config -
Cake\Datasource\EntityInterface|iterable|array|null$parent optional -
array<int, array<string, mixed>>$visibility optional Visibility of the nodes in
$data, seecalculateVisibility()
Returns
stringThrows
ExceptiongetConfig() ¶ public
getConfig(string|null $key = null, mixed $default = null): $key is null ? array : mixed
Returns the config.
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
-
string|null$key optional The key to get or null for the whole config.
-
mixed$default optional The return value when the key does not exist.
Returns
$key is null ? array : mixedConfiguration data at the named key or null if the key does not exist.
getConfigOrFail() ¶ public
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
Parameters
-
string$key The key to get.
Returns
mixedConfiguration data at the named key
Throws
InvalidArgumentExceptiongetView() ¶ public
getView(): TView
Get the view instance this helper is bound to.
Returns
TViewThe bound view instance.
implementedEvents() ¶ public
implementedEvents(): array<string, mixed>
Get the View callbacks this helper is interested in.
By defining one of the callback methods a helper is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want helpers to listen to non-standard events.
Returns
array<string, mixed>initialize() ¶ public
initialize(array<string, mixed> $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
Parameters
-
array<string, mixed>$config The configuration settings provided to this helper.
Returns
voidisFlat() ¶ protected
isFlat(array $nodes): bool
Whether the nodes are a flat MPTT result instead of nested children.
Parameters
-
array$nodes Nodes
Returns
boolsetConfig() ¶ public
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string$key The key to set, or a complete array of configs.
-
mixed|null$value optional The value to set.
-
bool$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
Returns
$thisThrows
Cake\Core\Exception\CakeExceptionWhen trying to set a key that is invalid.
threadedVisibility() ¶ protected
threadedVisibility(array $nodes, int $level, bool $parentShown): array<int, array{show: bool, hide: bool, children: array}>
Parameters
-
array$nodes Nodes
-
int$level Level
-
bool$parentShown Whether the parent is part of the tree path
Returns
array<int, array{show: bool, hide: bool, children: array}>Property Detail
$configInitialized ¶ protected
Whether the config property has already been configured with defaults
Type
bool$helperInstances ¶ protected
Loaded helper instances.
Type
array<string, \Cake\View\Helper<Cake\View\View>>$helpers ¶ protected
List of helpers used by this helper
Type
array<int|string, string|array<string, mixed>>