Skip to content

Commit

Permalink
Rewrite the DCA picker (see contao/core-bundle#950).
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp authored and leofeyer committed Jul 24, 2017
1 parent 8c85372 commit 4930053
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 359 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Contao news bundle change log

### DEV

* Rewrite the DCA picker (see contao/core-bundle#950).

### 4.4.1 (2017-07-12)

* Always show the "show from" and "show until" fields (see contao/core-bundle#908).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^5.6|^7.0",
"symfony/framework-bundle": "^3.3",
"contao/core-bundle": "^4.4"
"contao/core-bundle": "dev-hotfix/4.4.2"
},
"require-dev": {
"contao/manager-plugin": "^2.0",
Expand Down
115 changes: 0 additions & 115 deletions src/Menu/NewsPickerProvider.php

This file was deleted.

133 changes: 133 additions & 0 deletions src/Picker/NewsPickerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\NewsBundle\Picker;

use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\CoreBundle\Picker\AbstractPickerProvider;
use Contao\CoreBundle\Picker\DcaPickerProviderInterface;
use Contao\CoreBundle\Picker\PickerConfig;
use Contao\NewsArchiveModel;
use Contao\NewsModel;

/**
* Provides the news picker.
*
* @author Andreas Schempp <https://github.com/aschempp>
*/
class NewsPickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface, FrameworkAwareInterface
{
use FrameworkAwareTrait;

/**
* {@inheritdoc}
*/
public function getName()
{
return 'newsPicker';
}

/**
* {@inheritdoc}
*/
public function supportsContext($context)
{
return 'link' === $context && $this->getUser()->hasAccess('news', 'modules');
}

/**
* {@inheritdoc}
*/
public function supportsValue(PickerConfig $config)
{
return false !== strpos($config->getValue(), '{{news_url::');
}

/**
* {@inheritdoc}
*/
public function getDcaTable()
{
return 'tl_news';
}

/**
* {@inheritdoc}
*/
public function getDcaAttributes(PickerConfig $config)
{
$attributes = ['fieldType' => 'radio'];

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

return $attributes;
}

/**
* {@inheritdoc}
*/
public function convertDcaValue(PickerConfig $config, $value)
{
return '{{news_url::'.$value.'}}';
}

/**
* {@inheritdoc}
*/
protected function getLinkClass()
{
return 'news';
}

/**
* {@inheritdoc}
*/
protected function getRouteParameters(PickerConfig $config)
{
$params = ['do' => 'news'];

if ($config->getValue() && false !== strpos($config->getValue(), '{{news_url::')) {
$value = str_replace(['{{news_url::', '}}'], '', $config->getValue());

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

return $params;
}

/**
* Returns the news archive ID.
*
* @param int $id
*
* @return int|null
*/
private function getNewsArchiveId($id)
{
/** @var NewsModel $newsAdapter */
$newsAdapter = $this->framework->getAdapter(NewsModel::class);

if (!(($newsModel = $newsAdapter->findById($id)) instanceof NewsModel)) {
return null;
}

if (!(($newsArchive = $newsModel->getRelated('pid')) instanceof NewsArchiveModel)) {
return null;
}

return $newsArchive->id;
}
}
12 changes: 6 additions & 6 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ services:
calls:
- ["setFramework", ["@contao.framework"]]

contao_news.listener.news_picker_provider:
class: Contao\NewsBundle\Menu\NewsPickerProvider
contao_news.picker.news_provider:
class: Contao\NewsBundle\Picker\NewsPickerProvider
public: false
arguments:
- "@router"
- "@request_stack"
- "@security.token_storage"
- "@knp_menu.factory"
calls:
- [setTokenStorage, ["@security.token_storage"]]
tags:
- { name: contao.picker_menu_provider, priority: 128 }
- { name: contao.picker_provider, priority: 128 }
20 changes: 9 additions & 11 deletions tests/DependencyInjection/ContaoNewsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Contao\NewsBundle\EventListener\InsertTagsListener;
use Contao\NewsBundle\EventListener\PreviewUrlConvertListener;
use Contao\NewsBundle\EventListener\PreviewUrlCreateListener;
use Contao\NewsBundle\Menu\NewsPickerProvider;
use Contao\NewsBundle\Picker\NewsPickerProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -128,15 +128,13 @@ public function testPreviewUrlConvertListener()
*/
public function testNewsPickerProvider()
{
$this->assertTrue($this->container->has('contao_news.listener.news_picker_provider'));
$this->assertTrue($this->container->has('contao_news.picker.news_provider'));

$definition = $this->container->getDefinition('contao_news.listener.news_picker_provider');
$definition = $this->container->getDefinition('contao_news.picker.news_provider');

$this->assertSame(NewsPickerProvider::class, $definition->getClass());
$this->assertFalse($definition->isPublic());
$this->assertSame('router', (string) $definition->getArgument(0));
$this->assertSame('request_stack', (string) $definition->getArgument(1));
$this->assertSame('security.token_storage', (string) $definition->getArgument(2));
$this->assertSame('knp_menu.factory', (string) $definition->getArgument(0));

$conditionals = $definition->getInstanceofConditionals();

Expand All @@ -145,13 +143,13 @@ public function testNewsPickerProvider()
/** @var ChildDefinition $childDefinition */
$childDefinition = $conditionals[FrameworkAwareInterface::class];

$methodCalls = $childDefinition->getMethodCalls();

$this->assertSame('setFramework', $methodCalls[0][0]);
$this->assertSame('setFramework', $childDefinition->getMethodCalls()[0][0]);

$tags = $definition->getTags();

$this->assertArrayHasKey('contao.picker_menu_provider', $tags);
$this->assertSame(128, $tags['contao.picker_menu_provider'][0]['priority']);
$this->assertSame('setTokenStorage', $definition->getMethodCalls()[0][0]);

$this->assertArrayHasKey('contao.picker_provider', $tags);
$this->assertSame(128, $tags['contao.picker_provider'][0]['priority']);
}
}
Loading

0 comments on commit 4930053

Please sign in to comment.