Skip to content

Commit

Permalink
add option to hide container block types
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Sep 9, 2014
1 parent 32534c6 commit dcbed71
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 7 additions & 1 deletion Block/BlockServiceManager.php
Expand Up @@ -139,15 +139,21 @@ public function getServices()
/**
* {@inheritdoc}
*/
public function getServicesByContext($context)
public function getServicesByContext($context, $includeContainers = true)
{
if (!array_key_exists($context, $this->contexts)) {
return array();
}

$services = array();

$containers = $this->container->getParameter('sonata.block.container.types');

foreach ($this->contexts[$context] as $name) {
if (!$includeContainers && in_array($name, $containers)) {
continue;
}

$services[$name] = $this->getService($name);
}

Expand Down
5 changes: 3 additions & 2 deletions Block/BlockServiceManagerInterface.php
Expand Up @@ -48,11 +48,12 @@ public function setServices(array $blockServices);
public function getServices();

/**
* @param string $name
* @param string $name
* @param boolean $includeContainers
*
* @return array
*/
public function getServicesByContext($name);
public function getServicesByContext($name, $includeContainers = true);

/**
* @param string $name
Expand Down
11 changes: 6 additions & 5 deletions Form/Type/ServiceListType.php
Expand Up @@ -63,26 +63,27 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'expanded' => false,
'choices' => function (Options $options, $previousValue) use ($manager) {
$types = array();
foreach ($manager->getServicesByContext($options['context']) as $code => $service) {
foreach ($manager->getServicesByContext($options['context'], $options['include_containers']) as $code => $service) {
$types[$code] = sprintf('%s - %s', $service->getName(), $code);
}

return $types;
},
'preferred_choices' => array(),
'empty_data' => function (Options $options) {
'preferred_choices' => array(),
'empty_data' => function (Options $options) {
$multiple = isset($options['multiple']) && $options['multiple'];
$expanded = isset($options['expanded']) && $options['expanded'];

return $multiple || $expanded ? array() : '';
},
'empty_value' => function (Options $options, $previousValue) {
'empty_value' => function (Options $options, $previousValue) {
$multiple = isset($options['multiple']) && $options['multiple'];
$expanded = isset($options['expanded']) && $options['expanded'];

return $multiple || $expanded || !isset($previousValue) ? null : '';
},
'error_bubbling' => false,
'error_bubbling' => false,
'include_containers' => false
));
}
}

0 comments on commit dcbed71

Please sign in to comment.