Skip to content

Commit

Permalink
Merge branch 'feature/allow-url-type' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Feature/allow url type

See merge request metamodels/core!301
  • Loading branch information
zonky2 committed Jul 17, 2024
2 parents b2b75ce + 36e4384 commit 5f148df
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ vendor/
composer.lock

# build
.phpunit.result.cache
/.cache
/.phpunit.result.cache
/.pdepend/*
/.phpcq/*
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"require": {
"php": "^8.1",
"ext-dom": "*",
"contao-community-alliance/dc-general": "^2.3.10",
"contao-community-alliance/dc-general": "^2.3.15",
"contao-community-alliance/events-contao-bindings": "^4.13.1",
"contao-community-alliance/meta-palettes": "^2.0.10",
"contao-community-alliance/translator": "^2.4.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,30 @@
use Doctrine\DBAL\Connection;
use MetaModels\IFactory;
use MetaModels\IMetaModel;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* This handles the rendering of models to labels.
*/
class JumpToListener extends AbstractAbstainingListener
{
private const DEFAULT_TYPE = UrlGeneratorInterface::ABSOLUTE_PATH;

Check failure on line 42 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::DEFAULT_TYPE" should have a declared type. (reported by psalm)

Check failure on line 42 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::DEFAULT_TYPE" should have a declared type. (reported by psalm)

private const TYPE_MAP = [

Check failure on line 44 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::TYPE_MAP" should have a declared type. (reported by psalm)

Check failure on line 44 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::TYPE_MAP" should have a declared type. (reported by psalm)
'absolute_url' => UrlGeneratorInterface::ABSOLUTE_URL,
'absolute_path' => UrlGeneratorInterface::ABSOLUTE_PATH,
'relative_path' => UrlGeneratorInterface::RELATIVE_PATH,
'network_path' => UrlGeneratorInterface::NETWORK_PATH,
];

private const TYPE_MAP_INVERSE = [

Check failure on line 51 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::TYPE_MAP_INVERSE" should have a declared type. (reported by psalm)

Check failure on line 51 in src/CoreBundle/EventListener/DcGeneral/Table/RenderSettings/JumpToListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

MissingClassConstType: Class constant "MetaModels\CoreBundle\EventListener\DcGeneral\Table\RenderSettings\JumpToListener::TYPE_MAP_INVERSE" should have a declared type. (reported by psalm)
UrlGeneratorInterface::ABSOLUTE_URL => 'absolute_url',
UrlGeneratorInterface::ABSOLUTE_PATH => 'absolute_path',
UrlGeneratorInterface::RELATIVE_PATH => 'relative_path',
UrlGeneratorInterface::NETWORK_PATH => 'network_path',
];

/**
* The MetaModel factory.
*
Expand Down Expand Up @@ -105,6 +122,7 @@ public function decodeValue(DecodePropertyValueForWidgetEvent $event)
foreach (\array_keys($languages) as $key) {
$newValue = '';
$filter = 0;
$type = self::TYPE_MAP_INVERSE[self::DEFAULT_TYPE];
if ($value) {
foreach ($value as $arr) {
if (!\is_array($arr)) {
Expand All @@ -114,6 +132,7 @@ public function decodeValue(DecodePropertyValueForWidgetEvent $event)
// Set the new value and exit the loop.
if (\in_array($key, $arr, true)) {
$newValue = '{{link_url::' . $arr['value'] . '}}';
$type = self::TYPE_MAP_INVERSE[$arr['type'] ?? self::DEFAULT_TYPE];
$filter = $arr['filter'];
break;
}
Expand All @@ -123,6 +142,7 @@ public function decodeValue(DecodePropertyValueForWidgetEvent $event)
// Build the new array.
$newValues[] = [
'langcode' => $key,
'type' => $type,
'value' => $newValue,
'filter' => $filter
];
Expand All @@ -148,6 +168,7 @@ public function encodeValue(EncodePropertyValueFromWidgetEvent $event)

foreach ($value as $k => $v) {
$value[$k]['value'] = \str_replace(['{{link_url::', '}}'], ['', ''], $v['value']);
$value[$k]['type'] = self::TYPE_MAP[$v['type']] ?? self::DEFAULT_TYPE;
}

$event->setValue(\serialize($value));
Expand Down Expand Up @@ -209,12 +230,27 @@ public function buildWidget(BuildWidgetEvent $event)
];
}

$extra['columnFields']['type']['options'] = $this->getUrlTypes();
$extra['columnFields']['filter']['options'] = $this->getFilterSettings($model);

$event->getProperty()->setExtra($extra);
}


private function getUrlTypes(): array
{
$result = [];
foreach (self::TYPE_MAP_INVERSE as $typeName) {
$result[$typeName] = $this->translator->trans(
'jumpTo_type.' . $typeName,
[],
'tl_metamodel_rendersettings'
);
}

return $result;
}

/**
* Retrieve the model filters for the MCW.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,26 @@
'exclude' => true,
'inputType' => 'justtextoption',
'eval' => [
'tl_class' => 'jumpTo_language',
'valign' => 'center'
]
],
'type' => [
'label' => 'jumpTo_type.label',
'description' => 'jumpTo_type.description',
'exclude' => true,
'inputType' => 'select',
'eval' => [
'tl_class' => 'jumpTo_type',
]
],
'value' => [
'label' => 'jumpTo_page.label',
'description' => 'jumpTo_page.description',
'exclude' => true,
'inputType' => 'text',
'eval' => [
'style' => 'width:90%;'
'tl_class' => 'jumpTo_page',
]
],
'filter' => [
Expand All @@ -308,7 +318,7 @@
'exclude' => true,
'inputType' => 'select',
'eval' => [
'style' => 'width:100%;',
'tl_class' => 'jumpTo_filter',
'includeBlankOption' => true,
'chosen' => true
]
Expand Down
26 changes: 26 additions & 0 deletions src/CoreBundle/Resources/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @author Sven Baumann <[email protected]>
* @author Carolina Koehn <[email protected]>
* @author Cliff Parnitzky <[email protected]>
* @author Christian Schiffler <[email protected]>
* @copyright 2012-2024 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
Expand Down Expand Up @@ -164,3 +165,28 @@ form[id^=mm_] .sort_hint {
.tl_formbody_edit .settings {
margin: 0 0 10px 12px;
}

#tl_metamodel_rendersettings .jumpTo_language {
width: 15%;
}
#tl_metamodel_rendersettings .jumpTo_type {
width: 15%;
}
#tl_metamodel_rendersettings .jumpTo_type select {
width: 100%;
}
#tl_metamodel_rendersettings .jumpTo_page {
width: 15%;
}
#tl_metamodel_rendersettings .jumpTo_page input {
width: 88%;
}
#tl_metamodel_rendersettings .jumpTo_filter {
width: 55%;
}
#tl_metamodel_rendersettings .jumpTo_filter select {
width: 100%;
}
#tl_metamodel_rendersettings .operations {
display: none;
}
32 changes: 30 additions & 2 deletions src/CoreBundle/Resources/public/scss/style.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2022 The MetaModels team.
* (c) 2012-2024 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -14,7 +14,8 @@
* @author Ingolf Steinhardt <[email protected]>
* @author Sven Baumann <[email protected]>
* @author Carolina Koehn <[email protected]>
* @copyright 2012-2022 The MetaModels team.
* @author Christian Schiffler <[email protected]>
* @copyright 2012-2024 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand Down Expand Up @@ -144,3 +145,30 @@ form[id^=mm_] .sort_hint {
.tl_formbody_edit .settings {
margin: 0 0 10px 12px;
}

#tl_metamodel_rendersettings {
.jumpTo_language {
width: 15%;
}
.jumpTo_type {
width: 15%;
select {
width: 100%;
}
}
.jumpTo_page {
width: 15%;
input {
width: 88%;
}
}
.jumpTo_filter {
width: 55%;
select {
width: 100%;
}
}
.operations {
display: none;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@
<trans-unit id="jumpTo_language.description" resname="jumpTo_language.description">
<source>The language for the jump to page.</source>
</trans-unit>
<trans-unit id="jumpTo_type.label" resname="jumpTo_type.label">
<source>URL type</source>
</trans-unit>
<trans-unit id="jumpTo_type.description" resname="jumpTo_type.description">
<source>The reference type to use when generating the URL.</source>
</trans-unit>
<trans-unit id="jumpTo_type.absolute_url" resname="jumpTo_type.absolute_url">
<source>Absolute URL, e.g. "https://example.com/dir/file".</source>
</trans-unit>
<trans-unit id="jumpTo_type.absolute_path" resname="jumpTo_type.absolute_path">
<source>Absolute path, e.g. "/dir/file".</source>
</trans-unit>
<trans-unit id="jumpTo_type.relative_path" resname="jumpTo_type.relative_path">
<source>Relative path based on the current request path, e.g. "../parent-file".</source>
</trans-unit>
<trans-unit id="jumpTo_type.network_path" resname="jumpTo_type.network_path">
<source>Network path, e.g. "//example.com/dir/file".</source>
</trans-unit>
<trans-unit id="jumpTo_page.label" resname="jumpTo_page.label">
<source>Jump to page</source>
</trans-unit>
Expand Down
9 changes: 5 additions & 4 deletions src/Filter/FilterUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ public function __construct(
/**
* Generate a frontend url.
*
* @param FilterUrl $filterUrl The filter URL.
* @param FilterUrl $filterUrl The filter URL.
* @param int $referenceType The url reference type.
*
* @return string
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function generate(FilterUrl $filterUrl): string
public function generate(FilterUrl $filterUrl, int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
$jumpTo = $filterUrl->getPage();

Expand Down Expand Up @@ -172,11 +173,11 @@ public function generate(FilterUrl $filterUrl): string
}

if ($this->hasLegacyRouting) {
return $this->urlGenerator->generate($jumpTo['alias'] . $url, $parameters);
return $this->urlGenerator->generate($jumpTo['alias'] . $url, $parameters, $referenceType);
}

$parameters['parameters'] = $url;
return $this->urlGenerator->generate('tl_page.' . $jumpTo['id'], $parameters);
return $this->urlGenerator->generate('tl_page.' . $jumpTo['id'], $parameters, $referenceType);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/IMetaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* @see MetaModelFactory::byId To instantiate a MetaModel by its ID.
*
* @see IFactory::getMetaModel To instantiate a MetaModel by its table name.
*
* @psalm-suppress DeprecatedInterface
*/
interface IMetaModel
{
Expand All @@ -44,8 +46,6 @@ interface IMetaModel
* @return IMetaModelsServiceContainer
*
* @deprecated Inject services via constructor or setter.
*
* @psalm-suppress DeprecatedInterface
*/
public function getServiceContainer();

Expand Down
2 changes: 0 additions & 2 deletions src/MetaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ public function getServiceContainer()
* @return MetaModel
*
* @deprecated Inject services via constructor or setter.
*
* @psalm-suppress DeprecatedInterface
*/
public function setServiceContainer($serviceContainer, $deprecationNotice = true)
{
Expand Down
14 changes: 12 additions & 2 deletions src/Render/Setting/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
use MetaModels\IMetaModel;
use MetaModels\ITranslatedMetaModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Base implementation for render settings.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Collection implements ICollection
{
Expand Down Expand Up @@ -278,12 +281,14 @@ private function lookupJumpTo(bool $translated, string $desired = null, string $
{
$jumpToPageId = '';
$filterSettingId = '';
$referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
foreach ((array) $this->get('jumpTo') as $jumpTo) {
$langCode = $jumpTo['langcode'] ?? null;
// If either desired language or fallback, keep the result.
if (!$translated || ($langCode === $desired) || ($langCode === $fallback)) {
$jumpToPageId = $jumpTo['value'] ?? '';
$filterSettingId = (string) ($jumpTo['filter'] ?? '');
$referenceType = (int) ($jumpTo['type'] ?? UrlGeneratorInterface::ABSOLUTE_PATH);
// If the desired language, break.
// Otherwise, try to get the desired one until all have been evaluated.
if (!$translated || ($desired === $jumpTo['langcode'])) {
Expand All @@ -302,6 +307,7 @@ private function lookupJumpTo(bool $translated, string $desired = null, string $
'pageDetails' => $pageDetails,
'filter' => $filterSettingId,
'filterSetting' => $filterSetting,
'referenceType' => $referenceType,
// Mask out the "all languages" language key (See #687).
'language' => $pageDetails['language'] ?? '',
'label' => $this->getJumpToLabel()
Expand All @@ -311,7 +317,7 @@ private function lookupJumpTo(bool $translated, string $desired = null, string $
/**
* {@inheritdoc}
*/
public function buildJumpToUrlFor(IItem $item)
public function buildJumpToUrlFor(IItem $item /**, int $referenceType */)
{
$information = $this->determineJumpToInformation();
if (empty($information['pageDetails'])) {
Expand Down Expand Up @@ -341,7 +347,11 @@ public function buildJumpToUrlFor(IItem $item)
$result['params'] = $parameterList;
$result['deep'] = !empty($filterUrl->getSlugParameters());

$result['url'] = $this->filterUrlBuilder->generate($filterUrl);
$result['url'] = $this->filterUrlBuilder->generate(
$filterUrl,
$information['referenceType']
?? ((1 < func_num_args()) ? (int) func_get_arg(1) : UrlGeneratorInterface::ABSOLUTE_PATH)
);

return $result;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Render/Setting/ICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public function getSettingNames();
/**
* Render a filter url for the given item.
*
* @param IItem $item The item to generate the filter url for.
* @param IItem $item The item to generate the filter url for.
* @param int $referenceType Optional reference type - mandatory from MetaModels 3.0 on.
*
* @return array
*/
public function buildJumpToUrlFor(IItem $item);
public function buildJumpToUrlFor(IItem $item /**, int $referenceType */);
}

0 comments on commit 5f148df

Please sign in to comment.