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 ebbac99 commit 88f2381
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 361 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Contao FAQ bundle change log

### DEV

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

### 4.4.0-RC2 (2017-06-12)

* Trigger all the callbacks in the toggleVisibility() methods (see contao/core-bundle#756).
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/FaqPickerProvider.php

This file was deleted.

133 changes: 133 additions & 0 deletions src/Picker/FaqPickerProvider.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\FaqBundle\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\FaqCategoryModel;
use Contao\FaqModel;

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

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

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

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

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

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

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

return $attributes;
}

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

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

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

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

if (null !== ($faqId = $this->getFaqCategoryId($value))) {
$params['table'] = 'tl_faq';
$params['id'] = $faqId;
}
}

return $params;
}

/**
* Returns the FAQ category ID.
*
* @param int $id
*
* @return int|null
*/
private function getFaqCategoryId($id)
{
/** @var FaqModel $faqAdapter */
$faqAdapter = $this->framework->getAdapter(FaqModel::class);

if (!(($faqModel = $faqAdapter->findById($id)) instanceof FaqModel)) {
return null;
}

if (!(($faqCategory = $faqModel->getRelated('pid')) instanceof FaqCategoryModel)) {
return null;
}

return $faqCategory->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_faq.listener.faq_picker_provider:
class: Contao\FaqBundle\Menu\FaqPickerProvider
contao_faq.picker.faq_provider:
class: Contao\FaqBundle\Picker\FaqPickerProvider
public: false
arguments:
- "@router"
- "@request_stack"
- "@security.token_storage"
- "@knp_menu.factory"
calls:
- [setTokenStorage, ["@security.token_storage"]]
tags:
- { name: contao.picker_menu_provider, priority: 64 }
- { name: contao.picker_provider, priority: 64 }
24 changes: 11 additions & 13 deletions tests/DependencyInjection/ContaoFaqExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\FaqBundle\DependencyInjection\ContaoFaqExtension;
use Contao\FaqBundle\EventListener\InsertTagsListener;
use Contao\FaqBundle\Menu\FaqPickerProvider;
use Contao\FaqBundle\Picker\FaqPickerProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -68,19 +68,17 @@ public function testInsertTagsListener()
}

/**
* Tests the contao_faq.listener.faq_picker_provider service.
* Tests the contao_faq.picker.faq_provider service.
*/
public function testFaqPickerProvider()
public function testEventPickerProvider()
{
$this->assertTrue($this->container->has('contao_faq.listener.faq_picker_provider'));
$this->assertTrue($this->container->has('contao_faq.picker.faq_provider'));

$definition = $this->container->getDefinition('contao_faq.listener.faq_picker_provider');
$definition = $this->container->getDefinition('contao_faq.picker.faq_provider');

$this->assertSame(FaqPickerProvider::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 @@ -89,13 +87,13 @@ public function testFaqPickerProvider()
/** @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(64, $tags['contao.picker_menu_provider'][0]['priority']);
$this->assertSame('setTokenStorage', $definition->getMethodCalls()[0][0]);

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

0 comments on commit 88f2381

Please sign in to comment.