CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget

Class TreeHelper

Helper to generate tree representations of MPTT or recursively nested data.

Namespace: Cake\View\Helper

Property Summary

  • $config protected
    array<string, mixed>

    Runtime config

  • $configInitialized protected
    bool

    Whether 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

  • 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... ...
      ... The processing order is indicated by the numbers in brackets. attributes are allways applied to the next type (2) to be generated to set properties of the holding type - pass 'previous' for the 4th param i.e. // Hide children (2) $tree->addTypeAttribute('style', 'display', 'hidden'); // give top level type (1) a class $tree->addTypeAttribute('class', 'hasHiddenGrandChildren', null, 'previous');
  • attributes() protected

    Attributes function.

    Logic to apply styles to tags.

  • calculateVisibility() protected

    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.

  • 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 (-1 for 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 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.

  • 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>|null

Helper 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 to be processed Note that the content of a 'node' is processed before generating its wrapping tag

Parameters
string $id optional

Id

string $key optional

Key

mixed $value optional

Value

Returns
void

addTypeAttribute() ¶ public

addTypeAttribute(string $id = '', string $key = '', mixed|null $value = null, string $previousOrNext = 'next'): void

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... ...
    ... The processing order is indicated by the numbers in brackets. attributes are allways applied to the next type (2) to be generated to set properties of the holding type - pass 'previous' for the 4th param i.e. // Hide children (2) $tree->addTypeAttribute('style', 'display', 'hidden'); // give top level type (1) a class $tree->addTypeAttribute('class', 'hasHiddenGrandChildren', null, 'previous');
    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
string

calculateVisibility() ¶ 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
void
Throws
Cake\Core\Exception\CakeException
if 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 : mixed

configShallow() ¶ 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
$this

configWrite() ¶ 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
void
Throws
Cake\Core\Exception\CakeException
if 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
$this

flatParents() ¶ 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
string

HTML representation of the passed data

Throws
Exception

generateSubTree() ¶ 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, see calculateVisibility()

Returns
string
Throws
Exception

getConfig() ¶ 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 : mixed

Configuration 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
mixed

Configuration data at the named key

Throws
InvalidArgumentException

getView() ¶ public

getView(): TView

Get the view instance this helper is bound to.

Returns
TView

The 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
void

isFlat() ¶ protected

isFlat(array $nodes): bool

Whether the nodes are a flat MPTT result instead of nested children.

Parameters
array $nodes

Nodes

Returns
bool

setConfig() ¶ 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
$this
Throws
Cake\Core\Exception\CakeException
When 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

$config ¶ protected

Runtime config

Type
array<string, mixed>

$configInitialized ¶ protected

Whether the config property has already been configured with defaults

Type
bool

$currentConfig ¶ protected

Type
array

$defaultConfig ¶ protected

Default settings

Type
array<string, mixed>

$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>>

$itemAttributes ¶ protected

Type
array

$typeAttributes ¶ protected

Type
array

$typeAttributesNext ¶ protected

Type
array
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs