Skip to content

Commit

Permalink
modified for 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
j25j5 committed Feb 7, 2024
1 parent 9e3ce5c commit 7fdf3c6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
vendor/
sftp-config.json
.vscode/ftp-sync.json
.vscode/
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vonheldenundgestalten/contao-autowrap",
"description": "Contao 4 bundle to wrap content elements for easier page layout and HTML organization.",
"description": "Contao 5.3 bundle to wrap content elements for easier page layout and HTML organization.",
"type": "contao-bundle",
"authors": [
{
Expand All @@ -12,20 +12,25 @@
"name": "Marko Todić",
"email": "drexplosiveweb@gmail.com",
"role": "Developer"
},
{
"name": "Jannis Jirschik",
"email": "jannis.jirschik@vhug.de",
"role": "Developer"
}
],
"require": {
"contao/core-bundle": "^4.9"
"contao/core-bundle": "^5.3"
},
"require-dev": {
"contao/manager-plugin": "^2.3.1"
},
"extra": {
"contao-manager-plugin": "Magmell\\Contao\\Autowrap\\ContaoManager\\Plugin"
"contao-manager-plugin": "VHUG\\Contao\\Autowrap\\ContaoManager\\Plugin"
},
"autoload": {
"psr-4": {
"Magmell\\Contao\\Autowrap\\": "src"
"VHUG\\Contao\\Autowrap\\": "src"
},
"classmap": [
"src/Resources/contao/"
Expand Down
43 changes: 21 additions & 22 deletions src/Hooks/ContentElementsAutowrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Magmell\Contao\Autowrap\Hooks;

use Contao\Config;
use Contao\System;
use Contao\ContentElement;
use Contao\ContentModel;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Symfony\Component\HttpFoundation\RequestStack;

class ContentElementsAutowrap
{
Expand Down Expand Up @@ -36,12 +39,11 @@ public function __construct()
*
* @param ContentModel $objRow
* @param string $strBuffer
* @param ContentElement $objElement
* @return string
*/
public function getContentElement($objRow, $strBuffer, $objElement)
public function getContentElement($objRow, $strBuffer)
{
if (TL_MODE !== 'FE')
if (!System::getContainer()->get('contao.routing.scope_matcher')->isFrontendRequest(System::getContainer()->get('request_stack')->getCurrentRequest() ?? Request::create('')))
{
return $strBuffer;
}
Expand All @@ -51,16 +53,15 @@ public function getContentElement($objRow, $strBuffer, $objElement)
return $strBuffer;
}

if ($objElement->type !== "alias" && in_array($objElement->type, $this->arrElementTypesToAutoWrap))
if ($objRow->type !== "alias" && in_array($objRow->type, $this->arrElementTypesToAutoWrap))
{

$blnWrapperStart = $this->wrapperStart($objElement);
$blnWrapperStart = $this->wrapperStart($objRow);
if ($blnWrapperStart)
{
$strBuffer = sprintf($this->wrapperStart, $objElement->type) . $strBuffer;
$strBuffer = sprintf($this->wrapperStart, $objRow->type) . $strBuffer;
}

if ($this->wrapperEnd($objElement))
if ($this->wrapperEnd($objRow))
{
$strBuffer .= $this->wrapperEnd;
}
Expand All @@ -72,10 +73,10 @@ public function getContentElement($objRow, $strBuffer, $objElement)
static::$elementsCount[count(static::$elementsCount) - 1] += 1;
}
}
elseif($objElement->type == "alias") {
elseif($objRow->type == "alias") {

// fetch the Alias element, and check if it's in the autowrap list, then start the logic
$objAliasElement = ContentModel::findByPk($objElement->cteAlias);
$objAliasElement = ContentModel::findByPk($objRow->cteAlias);

if (in_array($objAliasElement->type, $this->arrElementTypesToAutoWrap)) {

Expand All @@ -97,27 +98,25 @@ public function getContentElement($objRow, $strBuffer, $objElement)
}
}



return $strBuffer;
}

/**
* @param ContentElement $objElement
* @param ContentModel $objRow
* @return bool
*/
protected function wrapperStart($objElement)
protected function wrapperStart($objRow)
{
$objCte = ContentModel::findPublishedByPidAndTable($objElement->pid, $objElement->ptable);
$objCte = ContentModel::findPublishedByPidAndTable($objRow->pid, $objRow->ptable);
$arrCtes = $objCte->fetchAll();

foreach ($arrCtes as $k => $arrCte)
{
// Current element
if ($objElement->id == $arrCte['id'])
if ($objRow->id == $arrCte['id'])
{
// It is first element or previous element is not of the same type
if ($k === 0 || $arrCtes[$k - 1]['type'] !== $objElement->type)
if ($k === 0 || $arrCtes[$k - 1]['type'] !== $objRow->type)
{
return true;
}
Expand All @@ -128,21 +127,21 @@ protected function wrapperStart($objElement)
}

/**
* @param ContentElement $objElement
* @param ContentElement $objRow
* @return bool
*/
protected function wrapperEnd($objElement)
protected function wrapperEnd($objRow)
{
$objCte = ContentModel::findPublishedByPidAndTable($objElement->pid, $objElement->ptable);
$objCte = ContentModel::findPublishedByPidAndTable($objRow->pid, $objRow->ptable);
$arrCtes = $objCte->fetchAll();

foreach ($arrCtes as $k => $arrCte)
{
// Current element
if ($objElement->id == $arrCte['id'])
if ($objRow->id == $arrCte['id'])
{
// It is already last element or the next element is not of the same type
if ($k === (count($arrCtes) - 1) || (array_key_exists($k + 1, $arrCtes) && $arrCtes[$k + 1]['type'] !== $objElement->type))
if ($k === (count($arrCtes) - 1) || (array_key_exists($k + 1, $arrCtes) && $arrCtes[$k + 1]['type'] !== $objRow->type))
{
return true;
}
Expand Down

0 comments on commit 7fdf3c6

Please sign in to comment.