Skip to content

Commit

Permalink
Allow to limit the DCA pickers per context (contao#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 13, 2017
1 parent d16b6fe commit c06329a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 19 additions & 5 deletions src/Picker/PickerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ public function __construct(FactoryInterface $menuFactory, RouterInterface $rout
*/
public function addProvider(PickerProviderInterface $provider)
{
$this->providers[] = $provider;
$this->providers[$provider->getName()] = $provider;
}

/**
* {@inheritdoc}
*/
public function create(PickerConfig $config)
{
$providers = $this->providers;

if (is_array($allowed = $config->getExtra('providers'))) {
$providers = array_intersect_key($providers, array_flip($allowed));
}

$providers = array_filter(
$this->providers,
$providers,
function (PickerProviderInterface $provider) use ($config) {
return $provider->supportsContext($config->getContext());
}
Expand Down Expand Up @@ -105,9 +111,15 @@ public function createFromData($data)
/**
* {@inheritdoc}
*/
public function supportsContext($context)
public function supportsContext($context, array $allowed = null)
{
foreach ($this->providers as $provider) {
$providers = $this->providers;

if (null !== $allowed) {
$providers = array_intersect_key($providers, array_flip($allowed));
}

foreach ($providers as $provider) {
if ($provider->supportsContext($context)) {
return true;
}
Expand All @@ -121,7 +133,9 @@ public function supportsContext($context)
*/
public function getUrl($context, array $extras = [], $value = '')
{
if (!$this->supportsContext($context)) {
$providers = (isset($extras['providers']) && is_array($extras['providers'])) ? $extras['providers'] : null;

if (!$this->supportsContext($context, $providers)) {
return '';
}

Expand Down
5 changes: 3 additions & 2 deletions src/Picker/PickerBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function createFromData($data);
/**
* Returns whether the given context is supported.
*
* @param string $context
* @param string $context
* @param array|null $allowed
*
* @return bool
*/
public function supportsContext($context);
public function supportsContext($context, array $allowed = null);

/**
* Gets picker URL for given context and configuration.
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/contao/classes/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ public static function getDcaPickerWizard($extras, $table, $field, $inputName)
{
$context = 'link';
$extras = is_array($extras) ? $extras : [];
$providers = (isset($extras['providers']) && is_array($extras['providers'])) ? $extras['providers'] : null;

if (isset($extras['context']))
{
Expand All @@ -1097,7 +1098,7 @@ public static function getDcaPickerWizard($extras, $table, $field, $inputName)

$factory = \System::getContainer()->get('contao.picker.builder');

if (!$factory->supportsContext($context))
if (!$factory->supportsContext($context, $providers))
{
return '';
}
Expand Down

0 comments on commit c06329a

Please sign in to comment.