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

Add meta checkbox field type for rendering meta robots #128

Open
wants to merge 7 commits into
base: master-4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* NovaeZSEOBundle SeoMetadataBooleanFieldType.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <[email protected]>
* @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 = $hash['meta_content'] == "1" ? 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 <[email protected]>
* @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\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,47 @@
<?php
/**
* NovaeZSEOBundle SeoMetadataDefaultFieldType.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <[email protected]>
* @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,38 @@
<?php
/**
* NovaeZSEOBundle SeoMetadataFieldTypeInterface.
*
* @package Novactive\Bundle\eZSEOBundle
*
* @author Novactive <[email protected]>
* @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
*
* @return Meta
*/
public function fromHash($hash): Meta;

/**
* @param string $fieldType
*
* @return bool
*/
public function support(string $fieldType): bool;

/**
* @param FormBuilderInterface $builder
* @param array $params
*/
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 <[email protected]>
* @copyright 2021 Novactive
* @license https://github.com/Novactive/NovaeZSEOBundle/blob/master/LICENSE MIT Licence
*/

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

use eZ\Publish\Core\MVC\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
* @param ConfigResolverInterface $configResolver
*/
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 @@ -85,11 +85,14 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$formConfig = $fieldForm->getConfig();

$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);
}

$fieldForm
Expand All @@ -100,7 +103,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
MetasFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $fieldDefinition->getName($formConfig->getOption('languageCode')),
'label' => $fieldDefinition->getName($formConfig->getOption('languageCode'))
]
)
->setAutoInitialize(false)
Expand Down
27 changes: 15 additions & 12 deletions bundle/Core/FieldType/Metas/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
use eZ\Publish\Core\FieldType\Value as CoreValue;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use Novactive\Bundle\eZSEOBundle\Core\FieldType\MetaFieldConverter\SeoMetadataFieldTypeRegistry;
use Novactive\Bundle\eZSEOBundle\Core\Meta;

class Type extends FieldType
{
const IDENTIFIER = 'novaseometas';

/** @var SeoMetadataFieldTypeRegistry */
protected $metadataFieldTypeRegistry;

/**
* @var array
*/
Expand All @@ -33,6 +37,15 @@ class Type extends FieldType
],
];

/**
* Type constructor.
* @param SeoMetadataFieldTypeRegistry $metadataFieldTypeRegistry
*/
public function __construct(SeoMetadataFieldTypeRegistry $metadataFieldTypeRegistry)
{
$this->metadataFieldTypeRegistry = $metadataFieldTypeRegistry;
}

/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
Expand Down Expand Up @@ -179,18 +192,8 @@ public function fromHash($hash)
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 All @@ -207,7 +210,7 @@ public function toHash(SPIValue $value)
/* @var Meta $meta */
$hash[$meta->getName()] = [
'meta_name' => $meta->getName(),
'meta_content' => $meta->getContent(),
'meta_content' => $meta->getContent()
];
}

Expand Down
Loading