Skip to content

Commit 6ee7f95

Browse files
committed
IOTA-313 cp files from enhanced link field type
0 parents  commit 6ee7f95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+17715
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.php-cs-fixer.cache
2+
.phpunit.result.cache
3+
coverage
4+
var
5+
vendor

.php-cs-fixer.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
->exclude(['var', 'vendor'])
6+
->files()->name('*.php')
7+
;
8+
9+
$config = new PhpCsFixer\Config();
10+
return $config
11+
->setRules([
12+
'@PSR12' => true,
13+
'@PSR12:risky' => true,
14+
'@PhpCsFixer' => true,
15+
'@PhpCsFixer:risky' => true,
16+
17+
// Overrides for rules included in PhpCsFixer rule sets
18+
'concat_space' => ['spacing' => 'one'],
19+
'method_chaining_indentation' => false,
20+
'multiline_whitespace_before_semicolons' => false,
21+
'native_function_invocation' => ['include' => ['@all']],
22+
'no_superfluous_phpdoc_tags' => false,
23+
'no_unset_on_property' => false,
24+
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
25+
'php_unit_internal_class' => false,
26+
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
27+
'php_unit_test_class_requires_covers' => false,
28+
'phpdoc_align' => false,
29+
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
30+
'single_line_comment_style' => false,
31+
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments']],
32+
'yoda_style' => false,
33+
'php_unit_strict' => false,
34+
'php_unit_test_annotation' => false,
35+
36+
// Additional rules
37+
'return_assignment' => false,
38+
'date_time_immutable' => true,
39+
'declare_strict_types' => true,
40+
'global_namespace_import' => [
41+
'import_classes' => null,
42+
'import_constants' => true,
43+
'import_functions' => true,
44+
],
45+
'list_syntax' => ['syntax' => 'short'],
46+
'heredoc_indentation' => ['indentation' => 'same_as_start'],
47+
'mb_str_functions' => true,
48+
'native_constant_invocation' => true,
49+
'nullable_type_declaration_for_default_null_value' => true,
50+
'static_lambda' => true,
51+
'ternary_to_null_coalescing' => true,
52+
'use_arrow_functions' => true,
53+
])
54+
->setRiskyAllowed(true)
55+
->setFinder($finder)
56+
;

LICENSE

+339
Large diffs are not rendered by default.

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ibexa-fieldtype-enhanced-link
2+
3+
[![Downloads](https://img.shields.io/packagist/dt/netgen/ibexa-fieldtype-enhanced-link.svg)](https://packagist.org/packages/netgen/ibexa-fieldtype-enhanced-link)
4+
[![Latest stable](https://img.shields.io/packagist/v/netgen/ibexa-fieldtype-enhanced-link.svg)](https://packagist.org/packages/netgen/ibexa-fieldtype-enhanced-link)
5+
[![PHP](https://img.shields.io/badge/PHP-%E2%89%A5%207.4-%238892BF.svg)](https://www.php.net)
6+
[![Ibexa](https://img.shields.io/badge/Ibexa-%E2%89%A5%204.0-orange.svg)](https://www.ibexa.co)
7+
8+
Enhanced link field type for Ibexa CMS is a combination of URL and Relation field types with some additional features.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\DependencyInjection;
6+
7+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8+
use Symfony\Component\Config\Definition\ConfigurationInterface;
9+
10+
class Configuration implements ConfigurationInterface
11+
{
12+
public function getAlias(): string
13+
{
14+
return 'netgen_ibexa_fieldtype_enhanced_link';
15+
}
16+
17+
public function getConfigTreeBuilder(): TreeBuilder
18+
{
19+
return new TreeBuilder('netgen_ibexa_fieldtype_enhanced_link');
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\DependencyInjection;
6+
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\Config\Resource\FileResource;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
11+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13+
use Symfony\Component\Yaml\Yaml;
14+
15+
use function file_get_contents;
16+
17+
class NetgenIbexaFieldTypeEnhancedLinkExtension extends Extension implements PrependExtensionInterface
18+
{
19+
/**
20+
* @throws \Exception
21+
*/
22+
public function load(array $configs, ContainerBuilder $container): void
23+
{
24+
$loader = new YamlFileLoader(
25+
$container,
26+
new FileLocator(__DIR__ . '/../Resources/config'),
27+
);
28+
29+
$loader->load('services.yaml');
30+
}
31+
32+
public function prepend(ContainerBuilder $container): void
33+
{
34+
$this->prependKernelSettings($container);
35+
}
36+
37+
public function prependKernelSettings(ContainerBuilder $container): void
38+
{
39+
$configFile = __DIR__ . '/../Resources/config/kernel.yaml';
40+
$config = Yaml::parse(file_get_contents($configFile));
41+
$container->prependExtensionConfig('ibexa', $config);
42+
$container->addResource(new FileResource($configFile));
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\EventListener\UniversalDiscovery;
6+
7+
use Ibexa\AdminUi\UniversalDiscovery\Event\ConfigResolveEvent;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
use function array_intersect;
11+
use function array_values;
12+
use function in_array;
13+
14+
class AllowedContentTypes implements EventSubscriberInterface
15+
{
16+
public static function getSubscribedEvents(): array
17+
{
18+
return [
19+
ConfigResolveEvent::NAME => ['onUdwConfigResolve'],
20+
];
21+
}
22+
23+
public function onUdwConfigResolve(ConfigResolveEvent $event): void
24+
{
25+
$context = $event->getContext();
26+
$config = $event->getConfig();
27+
28+
if (!in_array($event->getConfigName(), ['single', 'multiple'], true)) {
29+
return;
30+
}
31+
32+
if (
33+
!isset($context['type'], $context['allowed_content_types'])
34+
|| 'object_relation' !== $context['type']
35+
) {
36+
return;
37+
}
38+
39+
if (!empty($config['allowed_content_types'])) {
40+
$intersection = array_values(
41+
array_intersect(
42+
$config['allowed_content_types'],
43+
$context['allowed_content_types'],
44+
),
45+
);
46+
47+
$config['allowed_content_types'] = empty($intersection)
48+
? null
49+
: $intersection;
50+
} else {
51+
$config['allowed_content_types'] = empty($context['allowed_content_types'])
52+
? null
53+
: $context['allowed_content_types'];
54+
}
55+
56+
$event->setConfig($config);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\EventListener\UniversalDiscovery;
6+
7+
use Ibexa\AdminUi\UniversalDiscovery\Event\ConfigResolveEvent;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
class StartingLocationId implements EventSubscriberInterface
11+
{
12+
public static function getSubscribedEvents(): array
13+
{
14+
return [
15+
ConfigResolveEvent::NAME => ['onUdwConfigResolve'],
16+
];
17+
}
18+
19+
public function onUdwConfigResolve(ConfigResolveEvent $event): void
20+
{
21+
$configName = $event->getConfigName();
22+
if ('single' !== $configName && 'multiple' !== $configName) {
23+
return;
24+
}
25+
26+
$context = $event->getContext();
27+
if (
28+
!isset($context['type'], $context['starting_location_id'])
29+
|| 'object_relation' !== $context['type']
30+
) {
31+
return;
32+
}
33+
34+
$config = $event->getConfig();
35+
$config['starting_location_id'] = $context['starting_location_id'];
36+
37+
$event->setConfig($config);
38+
}
39+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\Form\Field;
6+
7+
use Ibexa\ContentForms\FieldType\Mapper\AbstractRelationFormMapper;
8+
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
9+
use Symfony\Component\Form\FormInterface;
10+
11+
class FieldValueFormMapper extends AbstractRelationFormMapper
12+
{
13+
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data): void
14+
{
15+
$fieldDefinition = $data->fieldDefinition;
16+
$formConfig = $fieldForm->getConfig();
17+
$fieldSettings = $fieldDefinition->getFieldSettings();
18+
19+
$fieldForm->add(
20+
$formConfig->getFormFactory()->createBuilder()
21+
->create(
22+
'value',
23+
FieldValueType::class,
24+
[
25+
'required' => $fieldDefinition->isRequired,
26+
'label' => $fieldDefinition->getName(),
27+
'default_location' => $this->loadDefaultLocationForSelection(
28+
$fieldSettings['selectionRoot'],
29+
$fieldForm->getConfig()->getOption('location'),
30+
),
31+
'root_default_location' => $fieldSettings['rootDefaultLocation'] ?? false,
32+
'enable_suffix' => $fieldSettings['enableSuffix'] ?? false,
33+
'target_internal' => $fieldSettings['allowedTargetsInternal'] ?? [],
34+
'target_external' => $fieldSettings['allowedTargetsExternal'] ?? [],
35+
],
36+
)
37+
->setAutoInitialize(false)
38+
->getForm(),
39+
);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\IbexaFieldTypeEnhancedLinkBundle\Form\Field;
6+
7+
use Ibexa\Contracts\Core\Repository\FieldType;
8+
use Netgen\IbexaFieldTypeEnhancedLink\FieldType\Type;
9+
use Netgen\IbexaFieldTypeEnhancedLink\FieldType\Value;
10+
use Symfony\Component\Form\DataTransformerInterface;
11+
12+
class FieldValueTransformer implements DataTransformerInterface
13+
{
14+
private FieldType $fieldType;
15+
16+
public function __construct(FieldType $fieldType)
17+
{
18+
$this->fieldType = $fieldType;
19+
}
20+
21+
public function transform($value): ?array
22+
{
23+
if (!$value instanceof Value) {
24+
return null;
25+
}
26+
27+
if ($value->isTypeExternal()) {
28+
return [
29+
'link_type' => Type::LINK_TYPE_EXTERNAL,
30+
'url' => $value->reference,
31+
'label_external' => $value->label,
32+
'target_external' => $value->target,
33+
];
34+
}
35+
36+
if ($value->isTypeInternal()) {
37+
return [
38+
'link_type' => Type::LINK_TYPE_INTERNAL,
39+
'id' => $value->reference,
40+
'label_internal' => $value->label,
41+
'target_internal' => $value->target,
42+
'suffix' => $value->suffix,
43+
];
44+
}
45+
46+
return null;
47+
}
48+
49+
public function reverseTransform($value): ?Value
50+
{
51+
if ($value['link_type'] === Type::LINK_TYPE_INTERNAL) {
52+
return new Value(
53+
$value['id'],
54+
$value['label_internal'],
55+
$value['target_internal'],
56+
$value['suffix'] ?? null,
57+
);
58+
}
59+
60+
if ($value['link_type'] === Type::LINK_TYPE_EXTERNAL) {
61+
return new Value(
62+
$value['url'],
63+
$value['label_external'],
64+
$value['target_external'],
65+
);
66+
}
67+
68+
return $this->fieldType->getEmptyValue();
69+
}
70+
}

0 commit comments

Comments
 (0)