You are here

public function MappingFieldsHelper::setFieldMappingValues in Gutenberg 8.2

Set the entity field values based on the mapping field settings.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The Drupal entity to presave.

File

src/MappingFieldsHelper.php, line 68

Class

MappingFieldsHelper
Handles the mappingFields configuration manipulation.

Namespace

Drupal\gutenberg

Code

public function setFieldMappingValues(EntityInterface $entity) {
  $text_fields = UtilsController::getEntityTextFields($entity);
  if (count($text_fields) === 0) {
    return;
  }
  $field_content = $entity
    ->get($text_fields[0])
    ->getString();

  // Fetch only blocks with mapping fields.
  $block_parser = new BlockParser();
  $blocks = $block_parser
    ->parse($field_content, [
    $this,
    'filterMappingFieldsBlock',
  ]);

  // Let's build the field's array of values.
  $fields = [];
  $filter_formats = filter_formats();

  // For each block match.
  foreach ($blocks as $block) {

    // Get block attributes.
    $attributes = $block['attrs'];
    $content = trim($block['innerHTML']);
    foreach ($attributes['mappingFields'] as $mapping_field) {
      if (!isset($mapping_field['field'])) {

        // No field name specified.
        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;
      }

      // Value doesn't support array yet.
      if ($value && is_array($value)) {
        $value = $value[0];
      }
      if (is_string($value)) {

        // Strip the text if the 'no_strip' and 'text_format' attributes are
        // empty.
        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'];

          /*
           * Allow use of a specific text format if it's valid.
           * FIXME: Not a fan of setting the text format this way, but can't
           *  think of a more elegant solution for it at the moment.
           */
          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(),
      ]));
    }
  }
}