Skip to content

Commit

Permalink
Make the insert tags in picker providers configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Apr 24, 2019
1 parent ed2fec5 commit 136b258
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 26 deletions.
22 changes: 17 additions & 5 deletions calendar-bundle/src/Picker/EventPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class EventPickerProvider extends AbstractPickerProvider implements DcaPickerPro
{
use FrameworkAwareTrait;

protected const DEFAULT_INSERTTAG = '{{event_url::%s}}';

/**
* {@inheritdoc}
*/
Expand All @@ -45,7 +47,9 @@ public function supportsContext($context): bool
*/
public function supportsValue(PickerConfig $config): bool
{
return false !== strpos($config->getValue(), '{{event_url::');
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}

/**
Expand All @@ -68,7 +72,9 @@ public function getDcaAttributes(PickerConfig $config): array
}

if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{event_url::', '}}'], '', $config->getValue());
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

$attributes['value'] = str_replace($insertTagChunks, '', $config->getValue());
}

return $attributes;
Expand All @@ -79,7 +85,7 @@ public function getDcaAttributes(PickerConfig $config): array
*/
public function convertDcaValue(PickerConfig $config, $value): string
{
return '{{event_url::'.$value.'}}';
return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), $value);
}

/**
Expand All @@ -89,11 +95,17 @@ protected function getRouteParameters(PickerConfig $config = null): array
{
$params = ['do' => 'calendar'];

if (null === $config || !$config->getValue() || false === strpos($config->getValue(), '{{event_url::')) {
if (null === $config || $config->getValue()) {
return $params;
}

$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

if (!false === strpos($config->getValue(), $insertTagChunks[0])) {
return $params;
}

$value = str_replace(['{{event_url::', '}}'], '', $config->getValue());
$value = str_replace($insertTagChunks, '', $config->getValue());

if (null !== ($calendarId = $this->getCalendarId($value))) {
$params['table'] = 'tl_calendar_events';
Expand Down
5 changes: 5 additions & 0 deletions calendar-bundle/tests/Picker/EventPickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function testConvertsTheDcaValue(): void
$this->assertSame('{{event_url::5}}', $this->provider->convertDcaValue(new PickerConfig('link'), 5));
}

public function testConvertsTheDcaValueWithCustomInsertTag(): void
{
$this->assertSame('{{event_title::5}}', $this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{event_title::%s}}']), 5));
}

public function testAddsTableAndIdIfThereIsAValue(): void
{
/** @var CalendarModel|MockObject $calendarModel */
Expand Down
12 changes: 12 additions & 0 deletions core-bundle/src/Picker/AbstractPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ protected function getUser(): BackendUser
return $user;
}

/**
* Provides a shortcut to get the "insertTag" extra value for child classes.
*/
protected function getInsertTag(PickerConfig $config, string $default): string
{
if ($insertTag = $config->getExtra('insertTag')) {
return (string) $insertTag;
}

return $default;
}

/**
* Returns the routing parameters for the back end picker.
*
Expand Down
12 changes: 9 additions & 3 deletions core-bundle/src/Picker/ArticlePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ArticlePickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface
{
protected const DEFAULT_INSERTTAG = '{{article_url::%s}}';

/**
* {@inheritdoc}
*/
Expand All @@ -35,7 +37,9 @@ public function supportsContext($context): bool
*/
public function supportsValue(PickerConfig $config): bool
{
return false !== strpos($config->getValue(), '{{article_url::');
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}

/**
Expand All @@ -58,7 +62,9 @@ public function getDcaAttributes(PickerConfig $config): array
}

if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{article_url::', '}}'], '', $config->getValue());
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

$attributes['value'] = str_replace($insertTagChunks, '', $config->getValue());
}

return $attributes;
Expand All @@ -69,7 +75,7 @@ public function getDcaAttributes(PickerConfig $config): array
*/
public function convertDcaValue(PickerConfig $config, $value): string
{
return '{{article_url::'.$value.'}}';
return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), $value);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions core-bundle/src/Picker/FilePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class FilePickerProvider extends AbstractPickerProvider implements DcaPickerProv
{
use FrameworkAwareTrait;

protected const DEFAULT_INSERTTAG = '{{file::%s}}';

/**
* @var string
*/
Expand Down Expand Up @@ -64,7 +66,9 @@ public function supportsValue(PickerConfig $config): bool
return Validator::isUuid($value);
}

return false !== strpos($value, '{{file::') || 0 === strpos($value, $this->uploadPath);
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]) || 0 === strpos($value, $this->uploadPath);
}

/**
Expand Down Expand Up @@ -101,7 +105,8 @@ public function convertDcaValue(PickerConfig $config, $value): string
$filesModel = $filesAdapter->findByPath(rawurldecode($value));

if ($filesModel instanceof FilesModel) {
return '{{file::'.StringUtil::binToUuid($filesModel->uuid).'}}';

return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), StringUtil::binToUuid($filesModel->uuid));
}

return $value;
Expand Down Expand Up @@ -180,8 +185,11 @@ private function getLinkDcaAttributes(PickerConfig $config): array
$value = $config->getValue();

if ($value) {
if (false !== strpos($value, '{{file::')) {
$value = str_replace(['{{file::', '}}'], '', $value);

$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

if (false !== strpos($value, $insertTagChunks[0])) {
$value = str_replace($insertTagChunks, '', $value);
}

if (0 === strpos($value, $this->uploadPath.'/')) {
Expand Down
14 changes: 10 additions & 4 deletions core-bundle/src/Picker/PagePickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class PagePickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface
{
protected const DEFAULT_INSERTTAG = '{{link_url::%s}}';

/**
* {@inheritdoc}
*/
Expand All @@ -39,7 +41,9 @@ public function supportsValue(PickerConfig $config): bool
return is_numeric($config->getValue());
}

return false !== strpos($config->getValue(), '{{link_url::');
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}

/**
Expand Down Expand Up @@ -82,8 +86,10 @@ public function getDcaAttributes(PickerConfig $config): array
return $attributes;
}

if ($value && false !== strpos($value, '{{link_url::')) {
$attributes['value'] = str_replace(['{{link_url::', '}}'], '', $value);
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

if ($value && false !== strpos($value, $insertTagChunks[0])) {
$attributes['value'] = str_replace($insertTagChunks, '', $value);
}

return $attributes;
Expand All @@ -98,7 +104,7 @@ public function convertDcaValue(PickerConfig $config, $value)
return (int) $value;
}

return '{{link_url::'.$value.'}}';
return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), $value);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions core-bundle/tests/Picker/ArticlePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ public function testConvertsTheDcaValue(): void
{
$this->assertSame('{{article_url::5}}', $this->provider->convertDcaValue(new PickerConfig('link'), 5));
}

public function testConvertsTheDcaValueWithCustomInsertTag(): void
{
$this->assertSame('{{article_title::5}}', $this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{article_title::%s}}']), 5));
}
}
18 changes: 18 additions & 0 deletions core-bundle/tests/Picker/FilePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,22 @@ public function testConvertsTheDcaValue(): void
$this->provider->convertDcaValue(new PickerConfig('link'), '/foobar')
);
}

public function testConvertsTheDcaValueWithCustomInsertTag(): void
{
$this->assertSame(
'/foobar',
$this->provider->convertDcaValue(new PickerConfig('file', ['insertTag' => '{{file_name::%s}}']), '/foobar')
);

$this->assertSame(
'{{file_name::82243f46-a4c3-11e3-8e29-000c29e44aea}}',
$this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{file_name::%s}}']), '/foobar')
);

$this->assertSame(
'/foobar',
$this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{file_name::%s}}']), '/foobar')
);
}
}
6 changes: 6 additions & 0 deletions core-bundle/tests/Picker/PagePickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,10 @@ public function testConvertsTheDcaValue(): void
$this->assertSame(5, $this->provider->convertDcaValue(new PickerConfig('page'), 5));
$this->assertSame('{{link_url::5}}', $this->provider->convertDcaValue(new PickerConfig('link'), 5));
}

public function testConvertsTheDcaValueWithCustomInsertTag(): void
{
$this->assertSame(5, $this->provider->convertDcaValue(new PickerConfig('page', ['insertTag' => '{{link_url::%s|absolute}}']), 5));
$this->assertSame('{{link_url::5|absolute}}', $this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{link_url::%s|absolute}}']), 5));
}
}
23 changes: 18 additions & 5 deletions faq-bundle/src/Picker/FaqPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class FaqPickerProvider extends AbstractPickerProvider implements DcaPickerProvi
{
use FrameworkAwareTrait;

protected const DEFAULT_INSERTTAG = '{{faq_url::%s}}';

/**
* {@inheritdoc}
*/
Expand All @@ -45,7 +47,9 @@ public function supportsContext($context): bool
*/
public function supportsValue(PickerConfig $config): bool
{
return false !== strpos($config->getValue(), '{{faq_url::');
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}

/**
Expand All @@ -68,7 +72,10 @@ public function getDcaAttributes(PickerConfig $config): array
}

if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{faq_url::', '}}'], '', $config->getValue());

$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

$attributes['value'] = str_replace($insertTagChunks, '', $config->getValue());
}

return $attributes;
Expand All @@ -79,7 +86,7 @@ public function getDcaAttributes(PickerConfig $config): array
*/
public function convertDcaValue(PickerConfig $config, $value): string
{
return '{{faq_url::'.$value.'}}';
return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), $value);
}

/**
Expand All @@ -89,11 +96,17 @@ protected function getRouteParameters(PickerConfig $config = null): array
{
$params = ['do' => 'faq'];

if (null === $config || !$config->getValue() || false === strpos($config->getValue(), '{{faq_url::')) {
if (null === $config ||!$config->getValue()) {
return $params;
}

$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

if (false === strpos($config->getValue(), $insertTagChunks[0])) {
return $params;
}

$value = str_replace(['{{faq_url::', '}}'], '', $config->getValue());
$value = str_replace($insertTagChunks, '', $config->getValue());

if (null !== ($faqId = $this->getFaqCategoryId($value))) {
$params['table'] = 'tl_faq';
Expand Down
5 changes: 5 additions & 0 deletions faq-bundle/tests/Picker/FaqPickerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function testConvertsTheDcaValue(): void
$this->assertSame('{{faq_url::5}}', $this->provider->convertDcaValue(new PickerConfig('link'), 5));
}

public function testConvertsTheDcaValueWithCustomInsertTag(): void
{
$this->assertSame('{{faq_title::5}}', $this->provider->convertDcaValue(new PickerConfig('link', ['insertTag' => '{{faq_title::%s}}']), 5));
}

public function testAddsTableAndIdIfThereIsAValue(): void
{
/** @var FaqCategoryModel|MockObject $model */
Expand Down
22 changes: 17 additions & 5 deletions news-bundle/src/Picker/NewsPickerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class NewsPickerProvider extends AbstractPickerProvider implements DcaPickerProv
{
use FrameworkAwareTrait;

protected const DEFAULT_INSERTTAG = '{{news_url::%s}}';

/**
* {@inheritdoc}
*/
Expand All @@ -45,7 +47,9 @@ public function supportsContext($context): bool
*/
public function supportsValue(PickerConfig $config): bool
{
return false !== strpos($config->getValue(), '{{news_url::');
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}

/**
Expand All @@ -68,7 +72,9 @@ public function getDcaAttributes(PickerConfig $config): array
}

if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{news_url::', '}}'], '', $config->getValue());
$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

$attributes['value'] = str_replace($insertTagChunks, '', $config->getValue());
}

return $attributes;
Expand All @@ -79,7 +85,7 @@ public function getDcaAttributes(PickerConfig $config): array
*/
public function convertDcaValue(PickerConfig $config, $value): string
{
return '{{news_url::'.$value.'}}';
return sprintf($this->getInsertTag($config, self::DEFAULT_INSERTTAG), $value);
}

/**
Expand All @@ -89,11 +95,17 @@ protected function getRouteParameters(PickerConfig $config = null): array
{
$params = ['do' => 'news'];

if (null === $config || !$config->getValue() || false === strpos($config->getValue(), '{{news_url::')) {
if (null === $config || !$config->getValue()) {
return $params;
}

$insertTagChunks = explode('%s', $this->getInsertTag($config, self::DEFAULT_INSERTTAG), 2);

if (false === strpos($config->getValue(), $insertTagChunks[0])) {
return $params;
}

$value = str_replace(['{{news_url::', '}}'], '', $config->getValue());
$value = str_replace($insertTagChunks, '', $config->getValue());

if (null !== ($newsArchiveId = $this->getNewsArchiveId($value))) {
$params['table'] = 'tl_news';
Expand Down
Loading

0 comments on commit 136b258

Please sign in to comment.