Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Jan 3, 2022
2 parents b20c647 + a2ed081 commit e8df80a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"doctrine/doctrine-bundle": "^2.3.2",
"doctrine/mongodb-odm": "^2.2",
"doctrine/orm": "^2.9",
"jackalope/jackalope-doctrine-dbal": "^1.1",
"knplabs/knp-menu-bundle": "^3.0",
"liip/imagine-bundle": "^2.0",
"matthiasnoback/symfony-config-test": "^4.2",
Expand All @@ -83,11 +82,11 @@
"psalm/plugin-symfony": "^3.0",
"sonata-project/admin-bundle": "^4.0",
"sonata-project/block-bundle": "^4.0",
"sonata-project/classification-bundle": "^4.0.0-RC1",
"sonata-project/classification-bundle": "^4.0",
"sonata-project/doctrine-orm-admin-bundle": "^4.0",
"symfony/browser-kit": "^4.4 || ^5.3 || ^6.0",
"symfony/messenger": "^4.4 || ^5.3 || ^6.0",
"symfony/phpunit-bridge": "^5.3 || ^6.0",
"symfony/phpunit-bridge": "^6.0",
"symfony/security-csrf": "^4.4 || ^5.3 || ^6.0",
"vimeo/psalm": "^4.7.2"
},
Expand Down
92 changes: 54 additions & 38 deletions src/Admin/BaseMediaAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,25 @@ protected function configurePersistentParameters(): array
return $parameters;
}

$request = $this->getRequest();

// TODO: Change to $request->query->all('filter') when support for Symfony < 5.1 is dropped.
$filter = $this->getRequest()->query->all()['filter'] ?? [];
$filter = $request->query->all()['filter'] ?? [];

if (\is_array($filter) && \array_key_exists('context', $filter)) {
$context = $filter['context']['value'];
} else {
$context = $this->getRequest()->query->get('context', $this->pool->getDefaultContext());
$context = $request->query->get('context', $this->pool->getDefaultContext());
}

$providers = $this->pool->getProvidersByContext($context);
$provider = $this->getRequest()->query->get('provider');
$provider = $request->query->get('provider');

// if the context has only one provider, set it into the request
// so the intermediate provider selection is skipped
if (1 === \count($providers) && null === $provider) {
$provider = array_shift($providers)->getName();
$this->getRequest()->query->set('provider', $provider);
$request->query->set('provider', $provider);
}

// if there is a post server error, provider is not posted and in case of
Expand All @@ -108,7 +110,7 @@ protected function configurePersistentParameters(): array
$parameters['provider'] = $provider;
}

$categoryId = $this->getRequest()->query->get('category');
$categoryId = $request->query->get('category');

if (null !== $this->categoryManager && null !== $this->contextManager && null === $categoryId) {
$rootCategories = $this->categoryManager->getRootCategoriesForContext($this->contextManager->find($context));
Expand All @@ -122,42 +124,54 @@ protected function configurePersistentParameters(): array
return array_merge($parameters, [
'context' => $context,
'category' => $categoryId,
'hide_context' => $this->getRequest()->query->getBoolean('hide_context'),
'hide_context' => $request->query->getBoolean('hide_context'),
]);
}

protected function alterNewInstance(object $object): void
{
if ($this->hasRequest()) {
if ($this->getRequest()->isMethod('POST')) {
$uniqId = $this->getUniqId();

// TODO: Change to $request->query->all($uniqid)['providerName'] when support for Symfony < 5.1 is dropped.
$data = $this->getRequest()->request->all()[$uniqId];
\assert(\is_array($data));
$object->setProviderName($data['providerName']);
} else {
$object->setProviderName($this->getRequest()->query->get('provider'));
}
if (!$this->hasRequest()) {
return;
}

$request = $this->getRequest();

$context = $this->getRequest()->query->get('context');
$object->setContext($context);
$categoryId = $this->getPersistentParameter('category');
if ($request->isMethod('POST')) {
$uniqId = $this->getUniqId();

if (null !== $this->categoryManager && null !== $categoryId) {
$category = $this->categoryManager->find($categoryId);
// TODO: Change to $request->request->all($uniqid)['providerName'] when support for Symfony < 5.1 is dropped.
$data = $request->request->all()[$uniqId] ?? [];
\assert(\is_array($data));

if (null === $category) {
return;
}
$providerName = $data['providerName'];
} else {
$providerName = $request->query->get('provider');
}

$categoryContext = $category->getContext();
$context = $request->query->get('context');

if (null !== $categoryContext && $categoryContext->getId() === $context) {
$object->setCategory($category);
}
}
$object->setProviderName($providerName);
$object->setContext($context);

$categoryId = $this->getPersistentParameter('category');

if (null === $this->categoryManager || null === $categoryId) {
return;
}

$category = $this->categoryManager->find($categoryId);

if (null === $category) {
return;
}

$categoryContext = $category->getContext();

if (null === $categoryContext || $categoryContext->getId() !== $context) {
return;
}

$object->setCategory($category);
}

protected function configureListFields(ListMapper $list): void
Expand Down Expand Up @@ -189,14 +203,16 @@ protected function configureFormFields(FormMapper $form): void
$provider->buildCreateForm($form);
}

if (null !== $this->categoryManager) {
$form->add('category', ModelListType::class, [], [
'link_parameters' => [
'context' => $media->getContext(),
'hide_context' => true,
'mode' => 'tree',
],
]);
if (null === $this->categoryManager) {
return;
}

$form->add('category', ModelListType::class, [], [
'link_parameters' => [
'context' => $media->getContext(),
'hide_context' => true,
'mode' => 'tree',
],
]);
}
}
8 changes: 5 additions & 3 deletions src/Admin/GalleryAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ protected function configurePersistentParameters(): array

protected function alterNewInstance(object $object): void
{
if ($this->hasRequest()) {
$object->setContext($this->getRequest()->query->get('context'));
if (!$this->hasRequest()) {
return;
}

$object->setContext($this->getRequest()->query->get('context'));
}

protected function configureFormFields(FormMapper $form): void
Expand All @@ -76,7 +78,7 @@ protected function configureFormFields(FormMapper $form): void
->with('form_group.gallery', ['class' => 'col-md-9'])->end()
->with('form_group.options', ['class' => 'col-md-3'])->end();

$context = $this->getPersistentParameter('context') ?? $this->pool->getDefaultContext();
$context = $this->getPersistentParameter('context', $this->pool->getDefaultContext());

$formats = [];
foreach ($this->pool->getFormatNamesByContext($context) as $name => $options) {
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/ORM/MediaAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function configureDatagridFilters(DatagridMapper $filter): void

$providers = [];

$providerNames = $this->pool->getProviderNamesByContext($this->getPersistentParameter('context') ?? $this->pool->getDefaultContext());
$providerNames = $this->pool->getProviderNamesByContext($this->getPersistentParameter('context', $this->pool->getDefaultContext()));
foreach ($providerNames as $name) {
$providers[$name] = $name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/MediaAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function listAction(Request $request): Response
$pool = $this->container->get('sonata.media.pool');
\assert($pool instanceof Pool);

$context = $this->admin->getPersistentParameter('context') ?? $pool->getDefaultContext();
$context = $this->admin->getPersistentParameter('context', $pool->getDefaultContext());
}

$datagrid->setValue('context', null, $context);
Expand Down

0 comments on commit e8df80a

Please sign in to comment.