Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the insert tags in picker providers configurable #450

Merged
merged 10 commits into from Jul 5, 2019
@@ -24,6 +24,8 @@ class EventPickerProvider extends AbstractPickerProvider implements DcaPickerPro
{
use FrameworkAwareTrait;
protected const DEFAULT_INSERTTAG = '{{event_url::%s}}';
/**
* {@inheritdoc}
*/
@@ -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);

This comment has been minimized.

Copy link
@leofeyer

leofeyer Apr 30, 2019

Member

Can we encapsulate this in a getInsertTagChunk() method so we do not have to use multiple explode() calls?

This comment has been minimized.

Copy link
@Toflar

Toflar May 1, 2019

Author Member

I'm not even sure if that should be named insertTag or if that should be something else? I mean, in fact it's just whatever string you want to provide and we're going to replace '%s' with the value. I just don't know if there's any other use case other than an insert tag 馃槃 wdyt?

This comment has been minimized.

Copy link
@leofeyer

leofeyer May 2, 2019

Member

I just don't know if there's any other use case other than an insert tag

I cannot think of one.

This comment has been minimized.

Copy link
@Toflar

Toflar May 2, 2019

Author Member

Done 馃槃

return false !== strpos($config->getValue(), $insertTagChunks[0]);
}
/**
@@ -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;
@@ -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);
}
/**
@@ -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';
@@ -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 */
@@ -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->getExtraForProvider('insertTag', $this->getName())) {
return (string) $insertTag;
}
return $default;
}
/**
* Returns the routing parameters for the back end picker.
*
@@ -14,6 +14,8 @@
class ArticlePickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface
{
protected const DEFAULT_INSERTTAG = '{{article_url::%s}}';
/**
* {@inheritdoc}
*/
@@ -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]);
}
/**
@@ -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;
@@ -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);
}
/**
@@ -25,6 +25,8 @@ class FilePickerProvider extends AbstractPickerProvider implements DcaPickerProv
{
use FrameworkAwareTrait;
protected const DEFAULT_INSERTTAG = '{{file::%s}}';
/**
* @var string
*/
@@ -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);
}
/**
@@ -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;
@@ -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.'/')) {
@@ -14,6 +14,8 @@
class PagePickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface
{
protected const DEFAULT_INSERTTAG = '{{link_url::%s}}';
/**
* {@inheritdoc}
*/
@@ -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]);
}
/**
@@ -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;
@@ -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);
}
/**
@@ -71,6 +71,15 @@ public function getCurrent(): string
return $this->current;
}
/**
* Checks if there's an extra value for a given provider and if not,
* falls back to the general extra.
*/
public function getExtraForProvider(string $name, string $provider)
{
return $this->extras[$provider][$name] ?? $this->getExtra($name);
}
public function getExtra(string $name)
{
return $this->extras[$name] ?? null;
@@ -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));
}
}
@@ -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')
);
}
}
@@ -213,4 +213,15 @@ 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
{
// General insertTag extra
$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));
// Picker specific insertTag extra
$this->assertSame(5, $this->provider->convertDcaValue(new PickerConfig('page', ['pagePicker' => ['insertTag' => '{{specific_insert_tag::%s}}']]), 5));
$this->assertSame('{{specific_insert_tag::5}}', $this->provider->convertDcaValue(new PickerConfig('link', ['pagePicker' => ['insertTag' => '{{specific_insert_tag::%s}}']]), 5));
}
}
@@ -50,6 +50,17 @@ public function testCanReadAndWriteExtras(): void
$this->assertSame('bar', $this->config->getExtra('foo'));
}
public function testCanReadProviderExtras(): void
{
$this->config->setExtra('insertTag', '{{fallback}}');
$this->config->setExtra('foobarProvider', [
'insertTag' => '{{foobarProviderSpecific}}',
]);
$this->assertSame('{{fallback}}', $this->config->getExtraForProvider('insertTag', 'notExistingProvider'));
$this->assertSame('{{foobarProviderSpecific}}', $this->config->getExtraForProvider('insertTag', 'foobarProvider'));
}
public function testClonesTheCurrentObject(): void
{
$clone = $this->config->cloneForCurrent('new-alias');
@@ -24,6 +24,8 @@ class FaqPickerProvider extends AbstractPickerProvider implements DcaPickerProvi
{
use FrameworkAwareTrait;
protected const DEFAULT_INSERTTAG = '{{faq_url::%s}}';
/**
* {@inheritdoc}
*/
@@ -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]);
}
/**
@@ -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;
@@ -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);
}
/**
@@ -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';
@@ -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 */
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.