Commit 12eb0fa6 authored by Mathilde Dumond's avatar Mathilde Dumond Committed by Sascha Grossenbacher
Browse files

Issue #3245330 by mathilde_dumond: Support for GatherContent Components

parent a7d6f3bf
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -200,7 +200,7 @@ class MappingEditForm extends MappingEditFormBase {

        // We need to modify field for checkboxes and field instance for radios.
        foreach ($template['related']->structure->groups as $group) {
          foreach ($group->fields as $gcField) {
          foreach ($this->flattenGroup($group->fields) as $gcField) {
            $local_field_id = $this->mappingData[$group->id]['elements'][$gcField->id];
            if ($gcField->type === 'choice_checkbox') {
              if (!empty($local_field_id)) {
+3 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\gathercontent_ui\Form;

use Drupal\gathercontent_ui\Traits\MappingTrait;
use GatherContent\DataTypes\Template;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
@@ -22,6 +23,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 */
class MappingEditFormBase extends EntityForm implements ContainerInjectionInterface {

  use MappingTrait;

  /**
   * Flag for mapping if it's new.
   *
+2 −1
Original line number Diff line number Diff line
@@ -100,7 +100,8 @@ class MappingStepEdit extends MappingSteps {
        ];
      }

      foreach ($group->fields as $gc_field) {
      $flat_fields = $this->flattenGroup($group->fields);
      foreach ($flat_fields as $gc_field) {
        $d_fields = [];
        if (isset($formState->getTriggeringElement()['#name'])) {
          // We need different handling for changed group.
+7 −2
Original line number Diff line number Diff line
@@ -128,7 +128,8 @@ class MappingStepNew extends MappingSteps {
          ];
        }

        foreach ($group->fields as $gc_field) {
        $flat_fields = $this->flattenGroup($group->fields);
        foreach ($flat_fields as $gc_field) {
          $d_fields = [];
          if ($formState->getTriggeringElement()['#name'] !== 'content_type') {
            // We need different handling for changed fieldset.
@@ -152,12 +153,16 @@ class MappingStepNew extends MappingSteps {
          else {
            $d_fields = $this->filterFields($gc_field, $contentType, $entityType);
          }
          $default_value = NULL;
          if ($formState->hasValue($group->id) && $formState->getValue($group->id)['elements'][$gc_field->id]) {
            $default_value = $formState->getValue($group->id)['elements'][$gc_field->id];
          }
          $form['mapping'][$group->id]['elements'][$gc_field->id] = [
            '#type' => 'select',
            '#options' => $d_fields,
            '#title' => (!empty($gc_field->label) ? $gc_field->label : $gc_field->title),
            '#empty_option' => $this->t("Don't map"),
            '#default_value' => $formState->hasValue($group->id)['elements'][$gc_field->id] ? $formState->getValue($group->id)['elements'][$gc_field->id] : NULL,
            '#default_value' => $default_value,
            '#attributes' => [
              'class' => [
                'gathercontent-ct-element',
+37 −29
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\gathercontent_ui\Form\MappingEditSteps;

use Drupal\gathercontent_ui\Traits\MappingTrait;
use GatherContent\DataTypes\Element;
use GatherContent\DataTypes\ElementText;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
@@ -21,6 +22,7 @@ abstract class MappingSteps {

  use StringTranslationTrait;
  use DependencySerializationTrait;
  use MappingTrait;

  /**
   * Mapping object.
@@ -231,37 +233,18 @@ abstract class MappingSteps {
            continue;
          }

          $lang_code = 'und';
          if ($translatable) {
            if (
              ($groupType == 'content' &&
                in_array($this->template['related']->structure->groups[$groupId]->fields[$k]->type, ['text', 'guidelines'])
              ) ||
              !in_array($element, ${$groupType . '_fields'}[$group['language']])
            ) {
              ${$groupType . '_fields'}[$group['language']][] = $element;
            }
            else {
              if (!strpos($element, '||')) {
                $formState->setErrorByName($groupId,
                  $this->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
              }
            $lang_code = $group['language'];
          }
          }
          else {
            if (
              ($groupType == 'content'
                && in_array($this->template['related']->structure->groups[$groupId]->fields[$k]->type, ['text', 'guidelines'])
              ) ||
              !in_array($element, ${$groupType . '_fields'})
            ) {
              ${$groupType . '_fields'}[] = $element;
            }
            else {
              if (!strpos($element, '||')) {
          if (isset(${$groupType . '_fields'}[$lang_code]) && in_array($element, ${$groupType . '_fields'}[$lang_code])) {
            $formState->setErrorByName($groupId,
              $this->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
            continue;
          }
            }

          if ($groupType == 'content' && in_array($this->getFieldFromId($k, $groupId)->type, ['text', 'guidelines'])) {
            ${$groupType . '_fields'}[$lang_code][] = $element;
          }
        }
      }
@@ -294,6 +277,31 @@ abstract class MappingSteps {
    }
  }

  /**
   * Wrapper function for filterFieldsRecursively.
   *
   * Use for filtering only equivalent fields.
   *
   * @param string $id
   *   GatherContent id of the field to retrieve.
   * @param string $group_id
   *   GatherContent id of the group containing the field.
   *
   * @return \GatherContent\DataTypes\Element
   *   The GatherContent field..
   */
  protected function getFieldFromId($id, $group_id) {
    if ($group_id && !strpos($id, '/')) {
      return $this->template['related']->structure->groups[$group_id]->fields[$id];
    }
    if ($group_id && strpos($id, '/')) {
      $ids = explode('/', $id);
      $component = $this->template['related']->structure->groups[$group_id]->fields[$ids[0]];
      $children = $component->getChildrenFields();
      return $children[$ids[1]];
    }
  }

  /**
   * Wrapper function for filterFieldsRecursively.
   *
Loading