Skip to content

Commit

Permalink
Merge pull request #2 from zoglo/main
Browse files Browse the repository at this point in the history
Unlock Contao 5 / Symfony Bundle structure
  • Loading branch information
Sioweb authored Aug 22, 2024
2 parents dbcaee8 + 6190061 commit 290ff71
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 173 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{php,twig,yaml,yml}]
indent_style = space
indent_size = 4

[*.{html5,svg,min.css,min.js}]
insert_final_newline = false

[*.html5]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.gitignore export-ignore
/assets export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Composer
/composer.lock
/vendor/
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Contao Inhaltselemente verlinken
<h1 align="center">Contao Linked Elements</h1>
<div align="center">
<img src="assets/readme/linkElement.png">
</div>

Nach der Installation, kann jedes Inhaltselement verlinkt werden.
---

> Working with **Contao 4.13** and **Contao 5+** (PHP ^8.1)
---

## Description

Allows linking every content element by wrapping the content with an `<a>` tag
Binary file added assets/readme/linkElement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 30 additions & 30 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
{
"name": "sioweb/linkedelement",
"type": "contao-bundle",
"description": "LinkedElement for Contao 4",
"license": "MIT",
"authors": [
{
"name": "Sascha Weidner",
"email": "support@sioweb.de"
}
],
"require": {
"php": ">=5.5.0",
"contao/core-bundle": "^4"
},
"autoload": {
"psr-4": {
"Sioweb\\LinkedElementBundle\\": "src/"
},
"classmap": [
"src/Resources/contao/"
],
"exclude-from-classmap": [
"src/Resources/contao/config/",
"src/Resources/contao/dca/",
"src/Resources/contao/languages/",
"src/Resources/contao/templates/"
]
},
"extra": {
"contao-manager-plugin": "Sioweb\\LinkedElementBundle\\ContaoManager\\Plugin"
"name": "sioweb/linkedelement",
"type": "contao-bundle",
"description": "LinkedElement for Contao 4",
"license": "MIT",
"authors": [
{
"name": "Sascha Weidner",
"email": "support@sioweb.de"
}
],
"require": {
"php": "^8.1",
"contao/core-bundle": "^4.13 || ^5.0"
},
"autoload": {
"psr-4": {
"Sioweb\\LinkedElementBundle\\": "src/"
},
"classmap": [
"contao/"
],
"exclude-from-classmap": [
"contao/config/",
"contao/dca/",
"contao/languages/",
"contao/templates/"
]
},
"extra": {
"contao-manager-plugin": "Sioweb\\LinkedElementBundle\\ContaoManager\\Plugin"
}
}
9 changes: 9 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: true

Sioweb\LinkedElementBundle\:
resource: '../src/'
exclude: '../src/{DependencyInjection}'
34 changes: 34 additions & 0 deletions contao/dca/tl_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* @author Sascha Weidner <https://www.sioweb.de>
* @copyright Sioweb, Sascha Weidner
*/

use Contao\CoreBundle\DataContainer\PaletteManipulator;

// Extend the default palettes
$objPalette = PaletteManipulator::create()
->addField(['linkedElement', 'linkedElementTarget'], 'type_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('default', 'tl_content')
;

foreach ($GLOBALS['TL_DCA']['tl_content']['palettes'] as $name => $palette)
{
if (!is_array($palette))
{
$objPalette->applyToPalette($name, 'tl_content');
}
}

$GLOBALS['TL_DCA']['tl_content']['fields']['linkedElement'] = [
'exclude' => true,
'search' => true,
'inputType' => 'text',
'eval' => ['rgxp' => 'url', 'decodeEntities' => true, 'maxlength' => 2048, 'dcaPicker' => true, 'tl_class' => 'w50'],
'sql' => "varchar(2048) NOT NULL default ''"
];

$GLOBALS['TL_DCA']['tl_content']['fields']['linkedElementTarget'] = $GLOBALS['TL_DCA']['tl_content']['fields']['target'];
5 changes: 5 additions & 0 deletions contao/languages/de/tl_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

$GLOBALS['TL_LANG']['tl_content']['linkedElement'] = ['Element verlinken', 'Verlinkt das gesamte Element.'];
5 changes: 5 additions & 0 deletions contao/languages/en/tl_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

$GLOBALS['TL_LANG']['tl_content']['linkedElement'] = ['Link element', 'Links the entire element.'];
3 changes: 3 additions & 0 deletions contao/templates/ce_linkedElement.html5
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a href="<?= $this->linkedElement; ?>" class="ce_linkedElement block"<?= $this->linkedElementTarget ? ' target="_blank"' : '' ?>>
<?= $this->element; ?>
</a>
19 changes: 7 additions & 12 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
<?php

declare(strict_types=1);

namespace Sioweb\LinkedElementBundle\ContaoManager;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;

/**
* Plugin for the Contao Manager.
*
* @author Sascha Weidner <https://www.sioweb.de>
*/
use Sioweb\LinkedElementBundle\SiowebLinkedElementBundle;

class Plugin implements BundlePluginInterface
{
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create('Sioweb\LinkedElementBundle\SiowebLinkedElementBundle')
->setLoadAfter(['Contao\CoreBundle\ContaoCoreBundle'])
->setReplace(['sioweblinkedelement']),
BundleConfig::create(SiowebLinkedElementBundle::class)
->setReplace(['sioweblinkedelement'])
->setLoadAfter([ContaoCoreBundle::class])
];
}
}
19 changes: 19 additions & 0 deletions src/DependencyInjection/SiowebLinkedElementExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Sioweb\LinkedElementBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class SiowebLinkedElementExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
}
}
34 changes: 34 additions & 0 deletions src/EventListener/GetContentElementListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Sioweb\LinkedElementBundle\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\ContentElement;
use Contao\ContentModel;
use Contao\FrontendTemplate;

#[AsHook('getContentElement')]
class GetContentElementListener
{
public function __invoke(ContentModel $contentModel, string $buffer, $element): string
{
if ($contentModel->linkedElement)
{
$template = new FrontendTemplate('ce_linkedElement');
$template->setData($contentModel->row());
$template->element = preg_replace('/<a([^>]*)>([^<]*)\<\/a>/is', '<span$1>$2</span>', $buffer);

// @todo Use one regex: ( href="[^"]*"| target="[^"]*"| name="[^"]*"| rel="[^"]*")
$template->element = preg_replace('/<span([^h]*)href[^=]*=[^"]*"[^"]*"([^>]*)>/is', '<span$1$2>', $template->element);
$template->element = preg_replace('/<span([^h]*)name[^=]*=[^"]*"[^"]*"([^>]*)>/is', '<span$1$2>', $template->element);
$template->element = preg_replace('/<span([^h]*)target[^=]*=[^"]*"[^"]*"([^>]*)>/is', '<span$1$2>', $template->element);
$template->element = preg_replace('/<span([^h]*)rel[^=]*=[^"]*"[^"]*"([^>]*)>/is', '<span$1$2>', $template->element);

$buffer = $template->parse();
}

return $buffer;
}
}
45 changes: 0 additions & 45 deletions src/Resources/contao/classes/LinkedElement.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Resources/contao/config/config.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Resources/contao/dca/tl_content.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Resources/contao/languages/de/tl_content.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Resources/contao/templates/ce_linkedElement.html5

This file was deleted.

Loading

0 comments on commit 290ff71

Please sign in to comment.