28 code examples of PHP Oro\Component\Layout\Util\BlockUtils extracted from open source projects
public function testSetViewVarsFromOptions()
{
$view = new BlockView();
BlockUtils::setViewVarsFromOptions(
$view,
new Options(
[
'test_path' => 'http://example.com',
'test_route_name' => 'test_route',
'test_route_parameters' => ['foo' => 'bar'],
]
),
['test_route_name', 'test_path']
);
$this->assertEquals('http://example.com', $view->vars['test_path']);
$this->assertEquals('test_route', $view->vars['test_route_name']);
}
public function testProcessUrlWithPrefixAndPathAndRoute()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options([
'test_path' => 'http://example.com',
'test_route_name' => 'test_route',
'test_route_parameters' => ['foo' => 'bar'],
]),
false,
'test'
);
$this->assertEquals('http://example.com', $view->vars['test_path']);
$this->assertArrayNotHasKey('test_route_name', $view->vars);
$this->assertArrayNotHasKey('test_route_parameters', $view->vars);
}
public function testProcessUrlWithPathAndRoute()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options([
'path' => 'http://example.com',
'route_name' => 'test_route',
'route_parameters' => ['foo' => 'bar'],
])
);
$this->assertEquals('http://example.com', $view->vars['path']);
$this->assertArrayNotHasKey('route_name', $view->vars);
$this->assertArrayNotHasKey('route_parameters', $view->vars);
}
public function testProcessUrlWithPrefixAndRouteParameters()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['test_route_name' => 'test_route', 'test_route_parameters' => ['foo' => 'bar']]),
false,
'test'
);
$this->assertArrayNotHasKey('test_path', $view->vars);
$this->assertEquals('test_route', $view->vars['test_route_name']);
$this->assertEquals(new Options(['foo' => 'bar']), $view->vars['test_route_parameters']);
}
public function testProcessUrlWithRouteParameters()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['route_name' => 'test_route', 'route_parameters' => ['foo' => 'bar']])
);
$this->assertArrayNotHasKey('path', $view->vars);
$this->assertEquals('test_route', $view->vars['route_name']);
$this->assertEquals(new Options(['foo' => 'bar']), $view->vars['route_parameters']);
}
public function testProcessUrlWithPrefixAndRoute()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['test_route_name' => 'test_route']),
false,
'test'
);
$this->assertArrayNotHasKey('test_path', $view->vars);
$this->assertEquals('test_route', $view->vars['test_route_name']);
$this->assertEquals([], $view->vars['test_route_parameters']);
}
public function testProcessUrlWithRoute()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['route_name' => 'test_route'])
);
$this->assertArrayNotHasKey('path', $view->vars);
$this->assertEquals('test_route', $view->vars['route_name']);
$this->assertEquals([], $view->vars['route_parameters']);
}
public function testProcessUrlWithPrefixAndPath()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['test_path' => 'http://example.com']),
false,
'test'
);
$this->assertEquals('http://example.com', $view->vars['test_path']);
$this->assertArrayNotHasKey('test_route_name', $view->vars);
$this->assertArrayNotHasKey('test_route_parameters', $view->vars);
}
public function testProcessUrlWithPath()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(['path' => 'http://example.com'])
);
$this->assertEquals('http://example.com', $view->vars['path']);
$this->assertArrayNotHasKey('route_name', $view->vars);
$this->assertArrayNotHasKey('route_parameters', $view->vars);
}
public function testProcessUrlWithPrefixAndEmptyOptions()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options(),
false,
'test'
);
$this->assertArrayNotHasKey('test_path', $view->vars);
$this->assertArrayNotHasKey('test_route_name', $view->vars);
$this->assertArrayNotHasKey('test_route_parameters', $view->vars);
}
public function testProcessUrlWithEmptyOptions()
{
$view = new BlockView();
BlockUtils::processUrl(
$view,
new Options()
);
$this->assertArrayNotHasKey('path', $view->vars);
$this->assertArrayNotHasKey('route_name', $view->vars);
$this->assertArrayNotHasKey('route_parameters', $view->vars);
}
public function testProcessUrlShouldThrowExceptionIfRequiredAndEmptyOptions()
{
$this->expectException(\Symfony\Component\OptionsResolver\Exception\MissingOptionsException::class);
$this->expectExceptionMessage('Either "path" or "route_name" must be set.');
BlockUtils::processUrl(
new BlockView(),
new Options(),
true
);
}
public function testRegisterPlugin()
{
$view = new BlockView();
$view->vars['block_prefixes'] = ['block', 'container', '_my_container'];
BlockUtils::registerPlugin($view, 'my_plugin');
$this->assertEquals(
['block', 'container', 'my_plugin', '_my_container'],
$view->vars['block_prefixes']
);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions(
$view,
$options,
['visible', 'translation_domain', 'additional_block_prefixes', 'class_prefix']
);
// merge the passed variables with the existing ones
if (!empty($options['vars'])) {
foreach ($options['vars'] as $name => $value) {
$view->vars[$name] = $value;
}
}
// replace attributes if specified ('attr' variable always exists in a view because it is added by FormView)
if (isset($options['attr'])) {
$view->vars['attr'] = $options['attr'];
}
// add label text and attributes if specified
if (isset($options['label'])) {
$view->vars['label'] = $options->get('label', false);
$view->vars['label_attr'] = [];
if (isset($options['label_attr'])) {
$view->vars['label_attr'] = $options->get('label_attr', false);
}
}
// add core variables to the block view, like id, block type and variables required for rendering engine
$view->vars['id'] = $block->getId();
$view->vars['block_type'] = $block->getTypeName();
BlockUtils::populateComputedViewVars($view->vars, $block->getContext()->getHash());
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions(
$view,
$options,
['visible', 'translation_domain', 'additional_block_prefixes', 'class_prefix']
);
// merge the passed variables with the existing ones
if (!empty($options['vars'])) {
foreach ($options['vars'] as $name => $value) {
$view->vars[$name] = $value;
}
}
// replace attributes if specified ('attr' variable always exists in a view because it is added by FormView)
if (isset($options['attr'])) {
$view->vars['attr'] = $options['attr'];
}
// add label text and attributes if specified
if (isset($options['label'])) {
$view->vars['label'] = $options->get('label', false);
$view->vars['label_attr'] = [];
if (isset($options['label_attr'])) {
$view->vars['label_attr'] = $options->get('label_attr', false);
}
}
// add core variables to the block view, like id, block type and variables required for rendering engine
$view->vars['id'] = $block->getId();
$view->vars['block_type'] = $block->getTypeName();
BlockUtils::populateComputedViewVars($view->vars, $block->getContext()->getHash());
}
protected function populateComputed(array &$vars, string $contextHash): void
{
if (!\array_key_exists(self::CLASS_PREFIX, $vars)) {
$vars[self::CLASS_PREFIX] = null;
}
BlockUtils::populateComputedViewVars($vars, $contextHash);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['name', 'type', 'value', 'placeholder', 'required']);
if (isset($options['id'])) {
$view->vars['attr']['id'] = $options->get('id', false);
}
if (isset($options['required'])) {
$view->vars['attr']['required'] = $options->get('required', false);
}
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
$attributeProxy = $this->createAttributeProxy($options);
$view->vars['label'] = $this->attributeConfigurationProvider->getAttributeLabel($attributeProxy);
BlockUtils::setViewVarsFromOptions($view, $options, ['entity', 'value', 'fieldName', 'className']);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['group']);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['options']);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions(
$view,
$options,
['form_action', 'form_route_name', 'form_route_parameters', 'form_method', 'form_enctype']
);
parent::buildView($view, $block, $options);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, LayoutOptions $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['form_data', 'render_rest']);
parent::buildView($view, $block, $options);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
$formAccessor = $this->getFormAccessor($block->getContext(), $options);
$view->vars['form'] = $formAccessor->getView($options['field_path']);
BlockUtils::setViewVarsFromOptions($view, $options, ['field_path']);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['render_rest']);
parent::buildView($view, $block, $options);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['form', 'form_name', 'instance_name']);
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, [
'grid_name',
'grid_parameters',
'grid_render_parameters',
]);
$view->vars['split_to_cells'] = $options['split_to_cells'];
if (!empty($options['grid_scope'])) {
$view->vars['grid_scope'] = $options->get('grid_scope', false);
$view->vars['grid_full_name'] = $this->nameStrategy->buildGridFullName(
$view->vars['grid_name'],
$view->vars['grid_scope']
);
} else {
$view->vars['grid_full_name'] = $view->vars['grid_name'];
}
}
/**
* @inheritdoc
*/
public function finishView(BlockView $view, BlockInterface $block)
{
BlockUtils::registerPlugin($view, 'taggable_datagrid');
}
/**
* @inheritdoc
*/
public function buildView(BlockView $view, BlockInterface $block, Options $options)
{
BlockUtils::setViewVarsFromOptions($view, $options, ['enable_tagging']);
}