Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Apr 29, 2020
1 parent 3c899e4 commit b0a1570
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
43 changes: 22 additions & 21 deletions Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public function getDefaultToolbarActions(): array
];
}

public function createContentRichViews(
public function createViews(
string $entityClass,
string $editParentView,
?string $addParentView = null,
?array $toolbarActions = null,
?string $securityContext = null
?string $securityContext = null,
?array $toolbarActions = null
): array {
$classMetadata = $this->entityManager->getClassMetadata($entityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');
Expand All @@ -127,7 +127,11 @@ public function createContentRichViews(

$toolbarActions = $toolbarActions ?: $this->getDefaultToolbarActions();
$addToolbarActions = $toolbarActions;
$seoAndExcerptToolbarActions = ['save' => $toolbarActions['save']];

$seoAndExcerptToolbarActions = [];
if (isset($toolbarActions['save'])) {
$seoAndExcerptToolbarActions = ['save' => $toolbarActions['save']];
}

if (!$this->hasPermission($securityContext, PermissionTypes::EDIT)) {
unset($toolbarActions['save']);
Expand All @@ -147,14 +151,16 @@ public function createContentRichViews(
$views = [];

if ($this->hasPermission($securityContext, PermissionTypes::ADD)) {
if ($addParentView && is_subclass_of($dimensionContentClass, TemplateInterface::class)) {
$views[] = $this->createTemplateFormView(
$addParentView,
false,
$resourceKey,
$templateFormKey,
$addToolbarActions
);
if ($addParentView) {
if (is_subclass_of($dimensionContentClass, TemplateInterface::class)) {
$views[] = $this->createTemplateFormView(
$addParentView,
false,
$resourceKey,
$templateFormKey,
$addToolbarActions
);
}
}
}

Expand Down Expand Up @@ -239,21 +245,16 @@ public function createExcerptFormView(
->setParent($parentView);
}

/**
* @return PreviewFormViewBuilderInterface|FormViewBuilderInterface
*/
protected function createFormViewBuilder(string $name, string $path, bool $previewEnabled): ViewBuilderInterface
private function createFormViewBuilder(string $name, string $path, bool $previewEnabled): ViewBuilderInterface
{
if ($previewEnabled) {
$formViewBuilder = $this->viewBuilderFactory->createPreviewFormViewBuilder($name, $path);
} else {
$formViewBuilder = $this->viewBuilderFactory->createFormViewBuilder($name, $path);
return $this->viewBuilderFactory->createPreviewFormViewBuilder($name, $path);
}

return $formViewBuilder;
return $this->viewBuilderFactory->createFormViewBuilder($name, $path);
}

protected function hasPermission(?string $securityContext, string $permissionType): bool
private function hasPermission(?string $securityContext, string $permissionType): bool
{
if (!$securityContext) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function getDefaultToolbarActions(): array;
*
* @return ViewBuilderInterface[]
*/
public function createContentRichViews(
public function createViews(
string $entityClass,
string $editParentView,
?string $addParentView = null,
?array $toolbarActions = null,
?string $securityContext = null
?string $securityContext = null,
?array $toolbarActions = null
): array;

/**
Expand Down
3 changes: 1 addition & 2 deletions Tests/Application/ExampleTestBundle/Admin/ExampleAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ public function configureViews(ViewCollection $viewCollection): void
->setTitleProperty('name')
);

$viewBuilders = $this->contentViewBuilderFactory->createContentRichViews(
$viewBuilders = $this->contentViewBuilderFactory->createViews(
Example::class,
static::EDIT_TABS_VIEW,
static::ADD_TABS_VIEW,
null,
static::SECURITY_CONTEXT
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function createContentObjectProvider(
);
}

public function testCreateContentRichViews(): void
public function testCreateViews(): void
{
$securityChecker = $this->prophesize(SecurityCheckerInterface::class);

Expand All @@ -87,7 +87,7 @@ public function testCreateContentRichViews(): void

$contentViewBuilder = $this->createContentViewBuilder($entityManager->reveal(), $securityChecker->reveal());

$views = $contentViewBuilder->createContentRichViews(Example::class, 'edit_parent_key');
$views = $contentViewBuilder->createViews(Example::class, 'edit_parent_key');

$this->assertCount(3, $views);

Expand All @@ -103,7 +103,7 @@ public function testCreateContentRichViews(): void
$this->assertSame('edit_parent_key.excerpt', $views[2]->getName());
$this->assertSame('content_excerpt', $views[2]->getView()->getOption('formKey'));

$views = $contentViewBuilder->createContentRichViews(Example::class, 'edit_parent_key', 'add_parent_key');
$views = $contentViewBuilder->createViews(Example::class, 'edit_parent_key', 'add_parent_key');

$this->assertCount(4, $views);

Expand All @@ -124,7 +124,7 @@ public function testCreateContentRichViews(): void
$this->assertSame('content_excerpt', $views[3]->getView()->getOption('formKey'));
}

public function testCreateContentRichViewsWithPreview(): void
public function testCreateViewsWithPreview(): void
{
$securityChecker = $this->prophesize(SecurityCheckerInterface::class);

Expand Down Expand Up @@ -153,7 +153,7 @@ public function testCreateContentRichViewsWithPreview(): void
$previewObjectProviderRegistry
);

$views = $contentViewBuilder->createContentRichViews(Example::class, 'edit_parent_key');
$views = $contentViewBuilder->createViews(Example::class, 'edit_parent_key');

$this->assertCount(3, $views);
$this->assertInstanceOf(PreviewFormViewBuilderInterface::class, $views[0]);
Expand Down Expand Up @@ -255,7 +255,7 @@ public function getSecurityContextData(): array
*
* @dataProvider getSecurityContextData
*/
public function testCreateContentRichViewsWithSecurityContext(array $permissions, array $expectedTypes): void
public function testCreateViewsWithSecurityContext(array $permissions, array $expectedTypes): void
{
$securityChecker = $this->prophesize(SecurityCheckerInterface::class);

Expand All @@ -273,11 +273,10 @@ public function testCreateContentRichViewsWithSecurityContext(array $permissions
$securityChecker->hasPermission('test_context', $permissionType)->willReturn($permission);
}

$views = $contentViewBuilder->createContentRichViews(
$views = $contentViewBuilder->createViews(
Example::class,
'edit_parent_key',
'add_parent_key',
null,
'test_context'
);

Expand Down
14 changes: 10 additions & 4 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ to `ContentViewBuilderFactory` & `ContentViewBuilderFactoryInterface`.

The service has been renamed from `sulu_content.content_view_builder` to `sulu_content.content_view_builder_factory`.

The function `build` was replaced by `createContentRichViews` and additional functions has been introduced.
The function `build` was replaced by `createViews` and additional functions has been introduced.

The behaviour of the `createViews` function detects now the needed views: the template-view if the `TemplateInterface`
is implemented, the seo-view if the `SeoInterface` is implemented, and the excerpt-view if the `ExcerptInterface` is
implemented.

**before**:

__BEFORE:__
```php
$this->contentViewBuilder->build(
$viewCollection,
Expand All @@ -22,9 +27,10 @@ $this->contentViewBuilder->build(
);
```

__AFTER:__
**after**:

```php
$viewBuilders = $this->contentViewBuilderFactory->createContentRichViews(
$viewBuilders = $this->contentViewBuilderFactory->createViews(
Example::class,
Example::TEMPLATE_TYPE,
static::EDIT_TABS_VIEW,
Expand Down

0 comments on commit b0a1570

Please sign in to comment.