protected function addElementTableAttributes()
    {
        $segments = Craft::$app->getRequest()->getSegments();
        $actionSegment = $segments[count($segments) - 1] ?? null;

        $classes = [Entry::class, Category::class];

        foreach ($classes as $class) {

            // Register "Add child" attribute
            Event::on($class, Element::EVENT_REGISTER_TABLE_ATTRIBUTES, function (RegisterElementTableAttributesEvent $event) use ($actionSegment) {
                $event->tableAttributes['_childme_addChild'] = $actionSegment !== 'get-elements' ? Craft::t('child-me', 'Add child') : '';
            });

            // Get the HTML for that attribute
            Event::on($class, Element::EVENT_SET_TABLE_ATTRIBUTE_HTML, function (SetElementTableAttributeHtmlEvent $event) use ($class) {
                if ($event->attribute === '_childme_addChild') {

                    $html = '';

                    switch ($class) {
                        case 'craft\elements\Entry':

                            $entry = $event->sender;

                            if ($entry->section->type !== 'structure') {
                                break;
                            }

                            $maxLevels = $entry->section->maxLevels;
                            $visible = !$maxLevels || $entry->level < $maxLevels;

                            $newUrl = UrlHelper::cpUrl(implode('/', ['entries', $entry->section->handle, 'new']), [
                                'typeId' => $entry->type->id,
                                'parentId' => $entry->id,
                            ]);

                            $html = $this->getElementTableAttributeHtml($newUrl, $visible, [
                                'data-section="' . $entry->section->handle . '"',
                                'data-id="' . $entry->id . '"'
                            ]);

                            break;
                        case 'craft\elements\Category':

                            $category = $event->sender;
                            $maxLevels = $category->group->maxLevels;
                            $visible = !$maxLevels || $category->level < $maxLevels;

                            $newUrl = UrlHelper::cpUrl(implode('/', ['categories', $category->group->handle, 'new']), [
                                'parentId' => $category->id,
                            ]);

                            $html = $this->getElementTableAttributeHtml($newUrl, $visible);

                            break;
                    }

                    $event->html = $html;
                    $event->handled = true;

                }


            });
        }


    }
protected function addElementTableAttributes()
    {
        $segments = Craft::$app->getRequest()->getSegments();
        $actionSegment = $segments[count($segments) - 1] ?? null;

        $classes = [Entry::class, Category::class];

        foreach ($classes as $class) {

            // Register "Add child" attribute
            Event::on($class, Element::EVENT_REGISTER_TABLE_ATTRIBUTES, function (RegisterElementTableAttributesEvent $event) use ($actionSegment) {
                $event->tableAttributes['_childme_addChild'] = $actionSegment !== 'get-elements' ? Craft::t('child-me', 'Add child') : '';
            });

            // Get the HTML for that attribute
            Event::on($class, Element::EVENT_SET_TABLE_ATTRIBUTE_HTML, function (SetElementTableAttributeHtmlEvent $event) use ($class) {
                if ($event->attribute === '_childme_addChild') {

                    $html = '';

                    switch ($class) {
                        case 'craft\elements\Entry':

                            $entry = $event->sender;

                            if ($entry->section->type !== 'structure') {
                                break;
                            }

                            $maxLevels = $entry->section->maxLevels;
                            $visible = !$maxLevels || $entry->level < $maxLevels;

                            $newUrl = UrlHelper::cpUrl(implode('/', ['entries', $entry->section->handle, 'new']), [
                                'typeId' => $entry->type->id,
                                'parentId' => $entry->id,
                            ]);

                            $html = $this->getElementTableAttributeHtml($newUrl, $visible, [
                                'data-section="' . $entry->section->handle . '"',
                                'data-id="' . $entry->id . '"'
                            ]);

                            break;
                        case 'craft\elements\Category':

                            $category = $event->sender;
                            $maxLevels = $category->group->maxLevels;
                            $visible = !$maxLevels || $category->level < $maxLevels;

                            $newUrl = UrlHelper::cpUrl(implode('/', ['categories', $category->group->handle, 'new']), [
                                'parentId' => $category->id,
                            ]);

                            $html = $this->getElementTableAttributeHtml($newUrl, $visible);

                            break;
                    }

                    $event->html = $html;
                    $event->handled = true;

                }


            });
        }


    }
protected function addElementTableAttributes ( ) { $ segments = Craft :: $ app -> getRequest ( ) -> getSegments ( ) ; $ actionSegment = $ segments [ count ( $ segments ) - 1 ] ?? null ; $ classes = [ Entry :: class , Category :: class ] ; foreach ( $ classes as $ class ) { // Register "Add child" attribute Event :: on ( $ class , Element :: EVENT_REGISTER_TABLE_ATTRIBUTES , function ( RegisterElementTableAttributesEvent $ event ) use ( $ actionSegment ) { $ event -> tableAttributes [ '_childme_addChild' ] = $ actionSegment !== 'get-elements' ? Craft :: t ( 'child-me' , 'Add child' ) : '' ; } ) ; // Get the HTML for that attribute Event :: on ( $ class , Element :: EVENT_SET_TABLE_ATTRIBUTE_HTML , function ( SetElementTableAttributeHtmlEvent $ event ) use ( $ class ) { if ( $ event -> attribute === '_childme_addChild' ) { $ html = '' ; switch ( $ class ) { case 'craft\elements\Entry' : $ entry = $ event -> sender ; if ( $ entry -> section -> type !== 'structure' ) { break ; } $ maxLevels = $ entry -> section -> maxLevels ; $ visible = ! $ maxLevels || $ entry -> level < $ maxLevels ; $ newUrl = UrlHelper :: cpUrl ( implode ( '/' , [ 'entries' , $ entry -> section -> handle , 'new' ] ) , [ 'typeId' => $ entry -> type -> id , 'parentId' => $ entry -> id , ] ) ; $ html = $ this -> getElementTableAttributeHtml ( $ newUrl , $ visible , [ 'data-section="' . $ entry -> section -> handle . '"' , 'data-id="' . $ entry -> id . '"' ] ) ; break ; case 'craft\elements\Category' : $ category = $ event -> sender ; $ maxLevels = $ category -> group -> maxLevels ; $ visible = ! $ maxLevels || $ category -> level < $ maxLevels ; $ newUrl = UrlHelper :: cpUrl ( implode ( '/' , [ 'categories' , $ category -> group -> handle , 'new' ] ) , [ 'parentId' => $ category -> id , ] ) ; $ html = $ this -> getElementTableAttributeHtml ( $ newUrl , $ visible ) ; break ; } $ event -> html = $ html ; $ event -> handled = true ; } } ) ; } }
Add element table attributes
Add element table attributes