You are here

public function PatternDisplayFormTrait::getMappingForm in UI Patterns 8

Get mapping form.

Parameters

string $pattern_id: Pattern ID for which to print the mapping form for.

string $tag: Source field plugin tag.

array $context: Plugin context.

array $configuration: Default configuration coming form the host form.

Return value

array Mapping form.

1 call to PatternDisplayFormTrait::getMappingForm()
PatternDisplayFormTrait::buildPatternDisplayForm in src/Form/PatternDisplayFormTrait.php
Build pattern display form.

File

src/Form/PatternDisplayFormTrait.php, line 91

Class

PatternDisplayFormTrait
Trait PatternDisplayFormTrait.

Namespace

Drupal\ui_patterns\Form

Code

public function getMappingForm($pattern_id, $tag, array $context, array $configuration) {

  /** @var \Drupal\ui_patterns\Definition\PatternDefinition $pattern */
  $pattern = $this->patternsManager
    ->getDefinition($pattern_id);
  $elements = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Source'),
      $this
        ->t('Plugin'),
      $this
        ->t('Destination'),
      $this
        ->t('Weight'),
    ],
  ];
  $elements['#tabledrag'][] = [
    'action' => 'order',
    'relationship' => 'sibling',
    'group' => 'field-weight',
  ];
  $destinations = [
    '_hidden' => $this
      ->t('- Hidden -'),
  ] + $pattern
    ->getFieldsAsOptions();
  $fields = [];
  foreach ($this->sourceManager
    ->getFieldsByTag($tag, $context) as $field_name => $field) {
    $weight = (int) $this
      ->getDefaultValue($configuration, $field_name, 'weight');
    $fields[$field_name] = [
      'info' => [
        '#plain_text' => $field
          ->getFieldLabel(),
      ],
      'plugin' => [
        '#plain_text' => $field
          ->getPluginLabel(),
      ],
      'destination' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Destination for @field', [
          '@field' => $field
            ->getFieldLabel(),
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $this
          ->getDefaultValue($configuration, $field_name, 'destination'),
        '#options' => $destinations,
      ],
      'weight' => [
        '#type' => 'weight',
        '#default_value' => $weight,
        '#delta' => 20,
        '#title' => $this
          ->t('Weight for @field field', [
          '@field' => $field
            ->getFieldLabel(),
        ]),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'field-weight',
          ],
        ],
      ],
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
      '#weight' => $weight,
    ];
  }
  uasort($fields, [
    SortArray::class,
    'sortByWeightProperty',
  ]);
  return array_merge($elements, $fields);
}