Skip to content

Commit

Permalink
* ibexa4 : Add meta checkbox field type for rendering meta robots (#110)
Browse files Browse the repository at this point in the history
* * ibexa4 : Add meta checkbox field type for rendering meta robots
---------

Co-authored-by: Florian ALEXANDRE <f.alexandre@novactive.com>
  • Loading branch information
mbouchaala and Florian ALEXANDRE authored Jun 3, 2024
1 parent 7c04882 commit 4b8bf86
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* NovaeZSEOBundle SeoMetadataBooleanFieldType.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <novaseobundle@novactive.com>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter;

use Novactive\Bundle\eZSEOBundle\Core\Meta;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

class SeoMetadataBooleanFieldType extends SeoMetadataDefaultFieldType
{
public const IDENTIFIER = 'boolean';

public function fromHash($hash): Meta
{
$meta = parent::fromHash($hash);
$content = '1' == $hash['meta_content'] ? true : false;
$meta->setContent($content);

return $meta;
}

public function mapForm(FormBuilderInterface &$builder, array $params)
{
$option = [
'class' => 'form-control',
'false_values' => '0',
];
$builder->add(
'content',
CheckboxType::class,
array_merge($params, $option)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* NovaeZSEOBundle SeoMetadataChoiceFieldType.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <novaseobundle@novactive.com>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

class SeoMetadataChoiceFieldType extends SeoMetadataDefaultFieldType
{
public const IDENTIFIER = 'select';

public function mapForm(FormBuilderInterface &$builder, array $params)
{
$builder->add(
'content',
ChoiceType::class,
$params
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* NovaeZSEOBundle SeoMetadataDefaultFieldType.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <novaseobundle@novactive.com>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter;

use Novactive\Bundle\eZSEOBundle\Core\Meta;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class SeoMetadataDefaultFieldType implements SeoMetadataFieldTypeInterface
{
public const IDENTIFIER = 'text';

public function support(string $fieldType): bool
{
return static::IDENTIFIER === $fieldType;
}

public function fromHash($hash): Meta
{
$meta = new Meta();
$meta->setName($hash['meta_name']);
$meta->setFieldType(self::IDENTIFIER);
$content = $hash['meta_content'];
$meta->setContent($content);

return $meta;
}

public function mapForm(FormBuilderInterface &$builder, array $params)
{
$params['empty_data'] = '';
$builder->add(
'content',
TextType::class,
$params
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* NovaeZSEOBundle SeoMetadataFieldTypeInterface.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <novaseobundle@novactive.com>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter;

use Novactive\Bundle\eZSEOBundle\Core\Meta;
use Symfony\Component\Form\FormBuilderInterface;

interface SeoMetadataFieldTypeInterface
{
/**
* @param $hash
*/
public function fromHash($hash): Meta;

public function support(string $fieldType): bool;

public function mapForm(FormBuilderInterface &$builder, array $params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* NovaeZSEOBundle SeoMetadataFieldTypeRegistry.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <novaseobundle@novactive.com>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter;

use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class SeoMetadataFieldTypeRegistry
{
/** @var SeoMetadataFieldTypeInterface[] */
protected $metaFieldTypes;

/** @var ConfigResolverInterface */
protected $configResolver;

/**
* SeoMetadataFieldTypeRegistry constructor.
*
* @param SeoMetadataFieldTypeInterface[] $metaFieldTypes
*/
public function __construct(iterable $metaFieldTypes)
{
foreach ($metaFieldTypes as $metaFieldType) {
$this->addMetaFieldType($metaFieldType);
}
}

/**
* @required
*/
public function setConfigResolver(ConfigResolverInterface $configResolver): void
{
$this->configResolver = $configResolver;
}

public function addMetaFieldType(SeoMetadataFieldTypeInterface $metaFieldType): void
{
$this->metaFieldTypes[] = $metaFieldType;
}

public function fromHash($hash): array
{
$metasConfig = $this->configResolver->getParameter('fieldtype_metas', 'nova_ezseo');

$metas = [];
foreach ($hash as $hashItem) {
if (!is_array($hashItem)) {
continue;
}
$fieldConfig = $metasConfig[$hashItem['meta_name']] ?? null;
$fieldType = $fieldConfig['type'] ?? SeoMetadataDefaultFieldType::IDENTIFIER;
foreach ($this->metaFieldTypes as $metaFieldType) {
if (!$metaFieldType->support($fieldType)) {
continue;
}
$metas[] = $metaFieldType->fromHash($hashItem);
}
}

return $metas;
}

public function mapForm(FormBuilderInterface &$builder, array $params, string $fieldType)
{
foreach ($this->metaFieldTypes as $metaFieldType) {
if (!$metaFieldType->support($fieldType)) {
continue;
}
$metaFieldType->mapForm($builder, $params);
}
}
}
13 changes: 8 additions & 5 deletions bundle/Core/FieldType/Metas/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

class FormMapper implements FieldDefinitionFormMapperInterface, FieldValueFormMapperInterface
{
/** @var ConfigResolverInterface */
protected $configResolver;
protected ConfigResolverInterface $configResolver;

/**
* FormMapper constructor.
Expand Down Expand Up @@ -87,10 +86,14 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)

$metasConfig = $this->configResolver->getParameter('fieldtype_metas', 'nova_ezseo');

if (empty($data->value->metas)) {
foreach (array_keys($metasConfig) as $key) {
$data->value->metas[$key] = new Meta($key, '');
$metasData = $data->value->metas;
foreach ($metasConfig as $key => $meta) {
$content = isset($metasData[$key]) ? $metasData[$key]->getContent() : null;
$fieldType = $meta['type'];
if (isset($metasData[$key]) && '' != $metasData[$key]->getFieldType()) {
$fieldType = $metasData[$key]->getFieldType();
}
$data->value->metas[$key] = new Meta($key, $content, $fieldType);
}

// able to add a meta which is not existant on your draft but on the configuration yml
Expand Down
21 changes: 10 additions & 11 deletions bundle/Core/FieldType/Metas/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Ibexa\Core\FieldType\FieldType;
use Ibexa\Core\FieldType\ValidationError;
use Ibexa\Core\FieldType\Value as CoreValue;
use Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter\SeoMetadataFieldTypeRegistry;
use Novactive\Bundle\eZSEOBundle\Core\Meta;

class Type extends FieldType
Expand All @@ -35,6 +36,14 @@ class Type extends FieldType
],
];

protected SeoMetadataFieldTypeRegistry $metadataFieldTypeRegistry;

public function __construct(
SeoMetadataFieldTypeRegistry $metadataFieldTypeRegistry
) {
$this->metadataFieldTypeRegistry = $metadataFieldTypeRegistry;
}

/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*/
Expand Down Expand Up @@ -148,18 +157,8 @@ public function fromHash($hash): Value
if (!\is_array($hash)) {
return new Value([]);
}
$metas = [];
foreach ($hash as $hashItem) {
if (!\is_array($hashItem)) {
continue;
}
$meta = new Meta();
$meta->setName($hashItem['meta_name']);
$meta->setContent($hashItem['meta_content']);
$metas[] = $meta;
}

return new Value($metas);
return new Value($this->metadataFieldTypeRegistry->fromHash($hash));
}

/**
Expand Down
Loading

0 comments on commit 4b8bf86

Please sign in to comment.