Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6315: Added available languages to relations view variable in Relation and RelationList field types #51

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions src/lib/Form/Type/FieldType/AbstractRelationFieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\ContentForms\Form\Type\FieldType;

use Ibexa\Contracts\Core\Limitation\Target;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation;
use Ibexa\Contracts\Core\Repository\Values\User\LookupLimitationResult;
use Symfony\Component\Form\AbstractType;

abstract class AbstractRelationFieldType extends AbstractType
{
private ContentService $contentService;

private PermissionResolver $permissionResolver;

public function __construct(
ContentService $contentService,
PermissionResolver $permissionResolver
) {
$this->permissionResolver = $permissionResolver;
$this->contentService = $contentService;
}

/**
* @return array{
* contentInfo: \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo|null,
* contentType: \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType|null,
* contentId: int,
* languageCodes: array<string>,
* unauthorized: bool,
* }
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
protected function getRelationData(int $contentId): array
{
$contentInfo = null;
$contentType = null;
$languageCodes = [];
$unauthorized = false;

try {
$versionInfo = $this->contentService->loadVersionInfoById($contentId);
$contentInfo = $versionInfo->getContentInfo();
$contentType = $contentInfo->getContentType();
$languageCodes = $this->getAvailableLanguageCodes($contentInfo, $versionInfo->getLanguages());
} catch (UnauthorizedException $e) {
$unauthorized = true;
}

return [
'contentInfo' => $contentInfo,
'contentType' => $contentType,
'contentId' => $contentId,
'languageCodes' => $languageCodes,
'unauthorized' => $unauthorized,
];
}

/**
* @param array<\Ibexa\Contracts\Core\Repository\Values\Content\Language> $languages
*
* @return array<string>
*/
public function getAvailableLanguageCodes(ContentInfo $contentInfo, array $languages): array
{
$languageCodes = [];
foreach ($languages as $language) {
$languageCodes[] = $language->getLanguageCode();
}

$lookupLimitationResult = $this->getLookupLimitationResult($contentInfo, $languageCodes);
if (
empty($lookupLimitationResult->lookupPolicyLimitations)
&& empty($lookupLimitationResult->roleLimitations)
) {
return $languageCodes;
}

$limitationLanguageCodes = $this->getLimitationLanguageCodes($lookupLimitationResult);

return array_filter(
$languageCodes,
static function (string $languageCode) use ($limitationLanguageCodes): bool {
return in_array($languageCode, $limitationLanguageCodes, true);
}
);
}

private function getLookupLimitationResult(ContentInfo $contentInfo, array $languageCodes): LookupLimitationResult
{
return $this->permissionResolver->lookupLimitations(
'content',
'edit',
$contentInfo,
[
(new Target\Builder\VersionBuilder())->translateToAnyLanguageOf($languageCodes)->build(),
$contentInfo->getMainLocation(),
],
[Limitation::LANGUAGE]
);
}

/**
* @return array<string>
*/
private function getLimitationLanguageCodes(LookupLimitationResult $lookupLimitations): array
{
$limitationLanguageCodes = [];
foreach ($lookupLimitations->roleLimitations as $roleLimitation) {
$limitationLanguageCodes[] = $roleLimitation->limitationValues;
}

foreach ($lookupLimitations->lookupPolicyLimitations as $lookupPolicyLimitation) {
foreach ($lookupPolicyLimitation->limitations as $limitation) {
$limitationLanguageCodes[] = $limitation->limitationValues;
}
}

return !empty($limitationLanguageCodes)
? array_unique(array_merge(...$limitationLanguageCodes))
: $limitationLanguageCodes;
}
ciastektk marked this conversation as resolved.
Show resolved Hide resolved
}
39 changes: 2 additions & 37 deletions src/lib/Form/Type/FieldType/RelationFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
namespace Ibexa\ContentForms\Form\Type\FieldType;

use Ibexa\ContentForms\FieldType\DataTransformer\RelationValueTransformer;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Core\FieldType\Relation\Value;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand All @@ -24,24 +20,8 @@
/**
* Form Type representing ezobjectrelation field type.
*/
class RelationFieldType extends AbstractType
class RelationFieldType extends AbstractRelationFieldType
{
/** @var \Ibexa\Contracts\Core\Repository\ContentService */
private $contentService;

/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
private $contentTypeService;

/**
* @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
* @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
*/
public function __construct(ContentService $contentService, ContentTypeService $contentTypeService)
{
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
}

public function getName()
{
return $this->getBlockPrefix();
Expand Down Expand Up @@ -75,23 +55,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
return;
}
$contentId = $data->destinationContentId;
$contentInfo = null;
$contentType = null;
$unauthorized = false;

try {
$contentInfo = $this->contentService->loadContentInfo($contentId);
$contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId);
} catch (UnauthorizedException $e) {
$unauthorized = true;
}

$view->vars['relations'][$data->destinationContentId] = [
'contentInfo' => $contentInfo,
'contentType' => $contentType,
'unauthorized' => $unauthorized,
'contentId' => $contentId,
];
$view->vars['relations'][$contentId] = $this->getRelationData($contentId);
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
40 changes: 2 additions & 38 deletions src/lib/Form/Type/FieldType/RelationListFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
namespace Ibexa\ContentForms\Form\Type\FieldType;

use Ibexa\ContentForms\FieldType\DataTransformer\RelationListValueTransformer;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Core\FieldType\RelationList\Value;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand All @@ -24,24 +20,8 @@
/**
* Form Type representing ezobjectrelationlist field type.
*/
class RelationListFieldType extends AbstractType
class RelationListFieldType extends AbstractRelationFieldType
{
/** @var \Ibexa\Contracts\Core\Repository\ContentService */
private $contentService;

/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
private $contentTypeService;

/**
* @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
* @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
*/
public function __construct(ContentService $contentService, ContentTypeService $contentTypeService)
{
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
}

public function getName()
{
return $this->getBlockPrefix();
Expand Down Expand Up @@ -76,23 +56,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
}

foreach ($data->destinationContentIds as $contentId) {
$contentInfo = null;
$contentType = null;
$unauthorized = false;

try {
$contentInfo = $this->contentService->loadContentInfo($contentId);
$contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId);
} catch (UnauthorizedException $e) {
$unauthorized = true;
}

$view->vars['relations'][$contentId] = [
'contentInfo' => $contentInfo,
'contentType' => $contentType,
'unauthorized' => $unauthorized,
'contentId' => $contentId,
];
$view->vars['relations'][$contentId] = $this->getRelationData($contentId);
}
}

Expand Down