Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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,49 @@
<?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 function support(string $fieldType): bool
{
return 'boolean' === $fieldType;
}

public function fromHash($hash): Meta
{
$meta = new Meta();
$meta->setName($hash['meta_name']);
$meta->setFieldType($hash['meta_fieldtype']);
$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,45 @@
<?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 Novactive\Bundle\eZSEOBundle\Core\Meta;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

class SeoMetadataChoiceFieldType extends SeoMetadataDefaultFieldType
{

public function support(string $fieldType): bool
{
return 'select' === $fieldType;
}

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

return $meta;
}

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,45 @@
<?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 function support(string $fieldType): bool
{
return 'text' === $fieldType;
}

public function fromHash($hash): Meta
{
$meta = new Meta();
$meta->setName($hash['meta_name']);
$meta->setFieldType($hash['meta_fieldtype']);
$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 <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
*
* @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,65 @@
<?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 Symfony\Component\Form\FormBuilderInterface;

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

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

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

public function fromHash($hash): array
{
$metas = [];
foreach ($hash as $hashItem) {
if (!is_array($hashItem)) {
continue;
}
foreach ($this->metaFieldTypes as $metaFieldType) {
if (!$metaFieldType->support($hashItem['meta_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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function storeFieldData(VersionInfo $versionInfo, Field $field)
)->set(
$connection->quoteColumn('meta_content'),
$insertQuery->bindValue($meta['meta_content'], null, PDO::PARAM_STR)
)->set(
$connection->quoteColumn('meta_fieldtype'),
$insertQuery->bindValue($meta['meta_fieldtype'], null, PDO::PARAM_STR)
)->set(
$connection->quoteColumn('objectattribute_id'),
$insertQuery->bindValue($field->id, null, PDO::PARAM_INT)
Expand Down
26 changes: 15 additions & 11 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 @@ -208,6 +211,7 @@ public function toHash(SPIValue $value)
$hash[$meta->getName()] = [
'meta_name' => $meta->getName(),
'meta_content' => $meta->getContent(),
'meta_fieldtype' => $meta->getFieldType(),
];
}

Expand Down
Loading