[FEATURE] Separation of search result path to title, uri, linkTag 49/54149/7
authorJoerg Boesche <typo3@joergboesche.de>
Fri, 15 Sep 2017 13:34:53 +0000 (15:34 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Mon, 27 Nov 2017 18:44:00 +0000 (19:44 +0100)
For styling and individual html markup of the result of indexed_search
it is now possible to get the path information in separate keys.
Introduced keys: pathTitle, pathUri.

Resolves: #82260
Releases: master
Change-Id: Iacb0e155c36778a5f1d5ed96db55c35794f6d091
Reviewed-on: https://review.typo3.org/54149
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Jonathan IROULIN <joniroutypo3@gmail.com>
Tested-by: Jonathan IROULIN <joniroutypo3@gmail.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Documentation/Changelog/master/Feature-82260-SeparationOfSearchResultPathIntoTitleUriLink.rst [new file with mode: 0644]
typo3/sysext/indexed_search/Classes/Controller/SearchController.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-82260-SeparationOfSearchResultPathIntoTitleUriLink.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-82260-SeparationOfSearchResultPathIntoTitleUriLink.rst
new file mode 100644 (file)
index 0000000..7e1f599
--- /dev/null
@@ -0,0 +1,30 @@
+.. include:: ../../Includes.txt
+
+=========================================================================
+Feature: #82260 - Separation of search result path into title,uri,linkTag
+=========================================================================
+
+See :issue:`82260`
+
+Description
+===========
+
+For styling and individual html markup of the result of indexed_search it is now possible to get the path information
+in separate keys. Introduced keys: `pathTitle`, `pathUri`.
+Enclose the whole result in a link or show the destination (link) as string. (e.g. bootstrap list item).
+
+
+Impact
+======
+
+The protected method :php:`linkPage()` returns an array with the "uri" and "target".
+To build an ATag outside of the fluid template, you have to use the introduced wrapper :php:`linkPageATagWrap()`.
+
+Using the keys in your fluid template:
+.. code-block:: html
+<div class="path">{row.path}</div>
+<div class="path-title">{row.pathTitle}</div>
+<div class="path-uri">{row.pathUri}</div>
+
+
+.. index:: Frontend, ext:indexed_search, NotScanned
index f48a22d..eba34aa 100644 (file)
@@ -476,7 +476,10 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
                 // Suspicious, so linking to page instead...
                 $copiedRow = $row;
                 unset($copiedRow['cHashParams']);
-                $title = $this->linkPage($row['page_id'], htmlspecialchars($title), $copiedRow);
+                $title = $this->linkPageATagWrap(
+                    htmlspecialchars($title),
+                    $this->linkPage($row['page_id'], $copiedRow)
+                );
             }
         } else {
             // Else the page:
@@ -490,7 +493,10 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
                     $markUpSwParams['sword_list'][] = $d['sword'];
                 }
             }
-            $title = $this->linkPage($row['data_page_id'], htmlspecialchars($title), $row, $markUpSwParams);
+            $title = $this->linkPageATagWrap(
+                htmlspecialchars($title),
+                $this->linkPage($row['data_page_id'], $row, $markUpSwParams)
+            );
         }
         $resultData['title'] = $title;
         $resultData['icon'] = $this->makeItemTypeIcon($row['item_type'], '', $specRowConf);
@@ -510,17 +516,27 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
             if ($GLOBALS['TSFE']->config['config']['fileTarget']) {
                 $targetAttribute = ' target="' . htmlspecialchars($GLOBALS['TSFE']->config['config']['fileTarget']) . '"';
             }
+            $resultData['pathTitle'] = $row['data_filename'];
+            $resultData['pathUri'] = $row['data_filename'];
             $resultData['path'] = '<a href="' . htmlspecialchars($row['data_filename']) . '"' . $targetAttribute . '>' . htmlspecialchars($row['data_filename']) . '</a>';
         } else {
             $pathId = $row['data_page_id'] ?: $row['page_id'];
             $pathMP = $row['data_page_id'] ? $row['data_page_mp'] : '';
             $pathStr = $this->getPathFromPageId($pathId, $pathMP);
-            $resultData['path'] = $this->linkPage($pathId, $pathStr, [
-                'cHashParams' => $row['cHashParams'],
-                'data_page_type' => $row['data_page_type'],
-                'data_page_mp' => $pathMP,
-                'sys_language_uid' => $row['sys_language_uid']
-            ]);
+            $pathLinkData = $this->linkPage(
+                $pathId,
+                [
+                    'cHashParams' => $row['cHashParams'],
+                    'data_page_type' => $row['data_page_type'],
+                    'data_page_mp' => $pathMP,
+                    'sys_language_uid' => $row['sys_language_uid']
+                ]
+            );
+
+            $resultData['pathTitle'] = $pathStr;
+            $resultData['pathUri'] = $pathLinkData['uri'];
+            $resultData['path'] = $this->linkPageATagWrap($pathStr, $pathLinkData);
+
             // check if the access is restricted
             if (is_array($this->requiredFrontendUsergroups[$pathId]) && !empty($this->requiredFrontendUsergroups[$pathId])) {
                 $lockedIcon = GeneralUtility::getFileAbsFileName('EXT:indexed_search/Resources/Public/Icons/FileTypes/locked.gif');
@@ -1313,13 +1329,11 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
      * Links the $linkText to page $pageUid
      *
      * @param int $pageUid Page id
-     * @param string $linkText Title to link (must already be escaped for HTML output)
      * @param array $row Result row
      * @param array $markUpSwParams Additional parameters for marking up search words
-     * @return string <A> tag wrapped title string.
-     * @todo make use of the UriBuilder
+     * @return array
      */
-    protected function linkPage($pageUid, $linkText, $row = [], $markUpSwParams = [])
+    protected function linkPage($pageUid, $row = [], $markUpSwParams = [])
     {
         $pageLanguage = $GLOBALS['TSFE']->sys_language_content;
         // Parameters for link
@@ -1338,24 +1352,8 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
         if (!is_array($this->domainRecords[$pageUid])) {
             $this->getPathFromPageId($pageUid);
         }
-        $target = '';
-        // If external domain, then link to that:
-        if (!empty($this->domainRecords[$pageUid])) {
-            $scheme = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
-            $firstDomain = reset($this->domainRecords[$pageUid]);
-            $additionalParams = '';
-            if (is_array($urlParameters) && !empty($urlParameters)) {
-                $additionalParams = GeneralUtility::implodeArrayForUrl('', $urlParameters);
-            }
-            $uri = $scheme . $firstDomain . '/index.php?id=' . $pageUid . $additionalParams;
-            if ($target = $this->settings['detectDomainRecords.']['target']) {
-                $target = ' target="' . $target . '"';
-            }
-        } else {
-            $uriBuilder = $this->controllerContext->getUriBuilder();
-            $uri = $uriBuilder->setTargetPageUid($pageUid)->setTargetPageType($row['data_page_type'])->setUseCacheHash(true)->setArguments($urlParameters)->build();
-        }
-        return '<a href="' . htmlspecialchars($uri) . '"' . $target . '>' . $linkText . '</a>';
+
+        return $this->preparePageLink($pageUid, $row, $urlParameters);
     }
 
     /**
@@ -1589,6 +1587,56 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
     }
 
     /**
+     * Internal method to build the page uri and link target.
+     * @todo make use of the UriBuilder
+     *
+     * @param int $pageUid
+     * @param array $row
+     * @param array $urlParameters
+     * @return array
+     */
+    protected function preparePageLink(int $pageUid, array $row, array $urlParameters): array
+    {
+        $target = '';
+        // If external domain, then link to that:
+        if (!empty($this->domainRecords[$pageUid])) {
+            $scheme = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
+            $firstDomain = reset($this->domainRecords[$pageUid]);
+            $additionalParams = '';
+            if (is_array($urlParameters) && !empty($urlParameters)) {
+                $additionalParams = GeneralUtility::implodeArrayForUrl('', $urlParameters);
+            }
+            $uri = $scheme . $firstDomain . '/index.php?id=' . $pageUid . $additionalParams;
+            $target = $this->settings['detectDomainRecords.']['target'];
+        } else {
+            $uriBuilder = $this->controllerContext->getUriBuilder();
+            $uri = $uriBuilder->setTargetPageUid($pageUid)
+                ->setTargetPageType($row['data_page_type'])
+                ->setUseCacheHash(true)
+                ->setArguments($urlParameters)
+                ->build();
+        }
+
+        return ['uri' => $uri, 'target' => $target];
+    }
+
+    /**
+     * Create a tag for "path" key in search result
+     *
+     * @param string $linkText Link text (nodeValue)
+     * @param array $linkData
+     * @return string <A> tag wrapped title string.
+     */
+    protected function linkPageATagWrap(string $linkText, array $linkData): string
+    {
+        $target = !empty($linkData['target']) ? 'target="' . htmlspecialchars($linkData['target']) . '"' : '';
+
+        return '<a href="' . htmlspecialchars($linkData['uri']) . '" ' . $target . '>'
+            . htmlspecialchars($linkText)
+            . '</a>';
+    }
+
+    /**
      * Set the search word
      * @param string $sword
      */