MappingFieldsHelper.php in Gutenberg 8.2
File
src/MappingFieldsHelper.php
View source
<?php
namespace Drupal\gutenberg;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\gutenberg\Controller\UtilsController;
use Drupal\gutenberg\Parser\BlockParser;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MappingFieldsHelper implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $configFactory;
protected $logger;
public function __construct(ConfigFactoryInterface $config_factory, LoggerInterface $logger) {
$this->logger = $logger;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('logger.channel.gutenberg'));
}
public function setFieldMappingValues(EntityInterface $entity) {
$text_fields = UtilsController::getEntityTextFields($entity);
if (count($text_fields) === 0) {
return;
}
$field_content = $entity
->get($text_fields[0])
->getString();
$block_parser = new BlockParser();
$blocks = $block_parser
->parse($field_content, [
$this,
'filterMappingFieldsBlock',
]);
$fields = [];
$filter_formats = filter_formats();
foreach ($blocks as $block) {
$attributes = $block['attrs'];
$content = trim($block['innerHTML']);
foreach ($attributes['mappingFields'] as $mapping_field) {
if (!isset($mapping_field['field'])) {
continue;
}
$mapping_field_name = $mapping_field['field'];
if (!isset($fields[$mapping_field_name])) {
$fields[$mapping_field_name] = [];
}
if (isset($mapping_field['attribute'])) {
$value = $attributes[$mapping_field['attribute']];
}
else {
$value = $content;
}
if ($value && is_array($value)) {
$value = $value[0];
}
if (is_string($value)) {
if (empty($mapping_field['no_strip']) && empty($mapping_field['text_format'])) {
$allowed_tags = isset($mapping_field['allowed_tags']) ? $mapping_field['allowed_tags'] : '';
$value = strip_tags($value, $allowed_tags);
}
$value = Html::decodeEntities($value);
}
if (isset($mapping_field['property'])) {
if (isset($mapping_field['text_format'])) {
$text_format = $mapping_field['text_format'];
if (isset($filter_formats[$text_format])) {
$value = check_markup($value, $text_format);
$fields[$mapping_field_name]['format'] = $text_format;
}
}
$fields[$mapping_field_name][$mapping_field['property']] = $value;
}
else {
$fields[$mapping_field_name] = $value;
}
}
}
foreach ($fields as $key => $value) {
try {
$entity
->set($key, $value);
} catch (\Exception $e) {
$this->logger
->error($this
->t('Mapping field failed: @message', [
'@message' => $e
->getMessage(),
]));
}
}
}
public function filterMappingFieldsBlock(array $block) {
if (isset($block['attrs']['mappingFields'])) {
$mapping_fields = $block['attrs']['mappingFields'];
return $mapping_fields && is_array($mapping_fields);
}
return FALSE;
}
public function getMappedFields(array $template = NULL) {
$result = [];
if (empty($template)) {
return [];
}
foreach ($template as $block) {
if (isset($block[1]) && isset($block[1]->mappingFields)) {
foreach ($block[1]->mappingFields as $field) {
$item = [];
$item['field'] = $field->field;
if (isset($field->property)) {
$item['property'] = $field->property;
}
if (isset($field->attribute)) {
$item['attribute'] = $field->attribute;
}
$result[] = $item;
}
}
if (isset($block[2])) {
$result = array_merge($result, $this
->getMappedFields($block[2]));
}
}
return $result;
}
}