Back to top Back to top
Light Dark

Description

TYPO3 create anchor link on current page

added fields to tt_content

#
# Table structure for table 'tt_content'
#
CREATE TABLE tt_content (
    tx_custom_anchor varchar(255) DEFAULT '' NOT NULL,
	tx_custom_anchor_desc varchar(255) DEFAULT '' NOT NULL,
);

set TCA on Configuration/TCA/Overrides/tt_content.php

// TCA
    $GLOBALS['TCA']['tt_content']['types']['customtemplate_pi1'] = [
        'showitem'=> '--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.general;general, image, media, pi_flexform'
    ];

    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
        'tt_content',
        [
            'tx_custom_anchor' => [
                'exclude' => 1,
                'label' => 'Custom Anker',
                'config' => [
                    'type' => 'input',
                    'size' => '25',
                    'max' => '255',
                    'size' => 50,
                ],
            ],
            'tx_custom_anchor_desc' => [
                'exclude' => 1,
                'label' => 'Custom Anker Description',
                'config' => [
                    'type' => 'input',
                    'size' => '25',
                    'max' => '255',
                    'size' => 50,
                ],
            ],
        ]
    );

    $GLOBALS['TCA']['tt_content']['ctrl']['useColumnsForDefaultValues'] .= ',tx_custom_anchor,tx_custom_anchor_desc';
    $GLOBALS['TCA']['tt_content']['ctrl']['shadowColumnsForNewPlaceholders'] .= ',tx_custom_anchor,tx_custom_anchor_desc';

    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette(
        'tt_content',
        'appearanceLinks',
        'tx_custom_anchor,tx_custom_anchor_desc',
        'after:linkToTop'
    );

read DB tt_content CustomTemplate\DataProcessing\MenuContent

namespace Xp\CustomTemplate\DataProcessing;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

class MenuContent
{
    public function rendContentMenu()
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');

        // get lang: default,en, de
        $curentLanguage = $GLOBALS['TSFE']->languageService->lang;

        $languageAspect = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class)->getAspect('language');
        $curentLang = $languageAspect->getId();

        // removed from V10
        //$sysLanUid = $GLOBALS['TSFE']->sys_language_uid;

        $currentId = $GLOBALS['TSFE']->id;
        $url = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');

        $rows = $queryBuilder
            ->select('uid', 'CType', 'tx_custom_anchor', 'tx_custom_anchor_desc', 'sys_language_uid', 't3_origuid')
            ->from('tt_content')
            ->where(
                $queryBuilder->expr()->andX(
                    $queryBuilder->expr()->eq('pid', $currentId),
                    $queryBuilder->expr()->eq('sys_language_uid', $curentLang),
                    $queryBuilder->expr()->neq('tx_custom_anchor', '""')
                )
            )
            ->execute()
            ->fetchAll();

        $menu = '';
        foreach($rows as $row) {
            if($row['tx_custom_anchor']) {
                $menu .= '<li class="anchor-nav-li"><a href="'. $url .'#c'. $row['uid'] .'" title="'. $row['tx_custom_anchor_desc'] .'">'. $row['tx_custom_anchor'] .'</a></li>';

            }
        }

        // TYPO3 10!
        $extConf = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['custom_template'];
        // or do not use ext_conf_template.txt config
        //return $menu?'<ul class="anchor-nav">'.$menu.'</ul>':'';

        if($extConf['show']) {
            return $menu?'<ul class="anchor-nav">'.$menu.'</ul>':'';
        } else {
            return '';
        }
    }
}

ext_conf_template.txt

# Records
###########################

# cat=records; type=bool; label=show the anchor
show = 0
10 = USER_INT
10 {
   userFunc = Xp\CustomTemplate\DataProcessing\MenuContent->rendContentMenu
}

# or via CONTENT get DB data
20 = CONTENT
    20 {
        table = tt_content
        select {
            selectFields = uid,tx_custom_anchor,tx_custom_anchor_desc
            # pidInList.data = uid
            pidInList.data = TSFE:id
            where = (tx_custom_anchor != '')
            languageField = sys_language_uid
        }
        renderObj = COA
        renderObj {
            1 = TEXT
            1 {
                field = uid
                # index.php?id={field:pid}
                wrap = <a href="#c|" title=
                wrap.insertData = 1
            }
            2 = TEXT
            2 {
                field = tx_custom_anchor_desc
                wrap = "|">
            }
            3 = TEXT
            3 {
                field = tx_custom_anchor
                wrap = |</a>
            }
            wrap = <li class="anchor-nav-li">|</li>
        }