dereuromark / cakephp-tools

A CakePHP Tools plugin containing lots of useful helpers, behaviors, components, shells, ...
http://www.dereuromark.de
MIT License
336 stars 142 forks source link

TreeHelper: Undefined array key "activePathElement" when using "div" as both type and itemType #267

Closed LordSimal closed 2 years ago

LordSimal commented 2 years ago

When outputting

    echo $this->Tree->generate( $categories, [
      'element' => 'categorie_tree',
      'type' => 'div',
      'itemType' => 'div'
    ] );

I get the error

Warning (2): Undefined array key "activePathElement" [ROOT/vendor/dereuromark/cakephp-tools/src/View/Helper/TreeHelper.php, line 497]

Its not that big of a deal but I would prefer simple divs with custom classes (which are set in my element) instead of a ul-li structure.

My element template is

$this->Tree->addTypeAttribute('class', 'row row-wrap', null, 'previous');
if($depth > 0) {
  $this->Tree->addItemAttribute('class', 'column column-50');
} else {
  $this->Tree->addItemAttribute('class', 'column column-33');
}

?>

<div class="c-category-tree__entry">
  <div class="node-name"><?= $data->name; ?></div>
  <div class="actions">
    <?= $this->Html->link( '<i class="material-icons">remove_red_eye</i>', [ 'action' => 'view', $data->id ], [
      'class'       => 'button button-outline',
      'escapeTitle' => false
    ] ) ?>
    <?= $this->Html->link( '<i class="material-icons">create</i>', [ 'action' => 'edit', $data->id ], [
      'class'       => 'button button-outline',
      'escapeTitle' => false
    ] ) ?>
  </div>
</div>

CakePHP 4.2.10 Plugin Version: 2.3.0

dereuromark commented 2 years ago

Do you have an idea for a fix as PR?

LordSimal commented 2 years ago

I haven't doven into your recursive function yet 😅

LordSimal commented 2 years ago

The warning can easily be fixed with a isset() but the printed HTML is not correct. image

Left is with

    echo $this->Tree->generate( $categories, [
      'element' => 'categorie_tree',
      'type' => 'div',
      'itemType' => 'div'
    ] );

Right is with

    echo $this->Tree->generate( $categories, [
      'element' => 'categorie_tree',
      //'type' => 'div',
      'itemType' => 'div'
    ] );
dereuromark commented 2 years ago

I usually used a callback directly, or method. If you find a way to make it work, though, a PR is welcome.

LordSimal commented 2 years ago

Well here is some more info:

with both 'type' => 'div', 'itemType' => 'div' the functions addTypeAttribute() and addItemAttribute() behave differently.

addTypeAttribute now adds the class to the item, not the wrapper around it and addItemAttribute doesn't do anything at all

LordSimal commented 2 years ago

The problem seems to be, that this condition is not good when type and itemType are both the same. https://github.com/dereuromark/cakephp-tools/blob/master/src/View/Helper/TreeHelper.php#L484

LordSimal commented 2 years ago

It seems like I got a fix. I will create a PR