You are here

protected function SettingsForm::buildMappingForm in Instagram Feeds 8

Form builder for mapping between media entity and Instagram post.

Parameters

array $form: Form array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

string $field_name: Parent field name to build the form tree.

Return value

array Fields mapping form element array.

1 call to SettingsForm::buildMappingForm()
SettingsForm::buildForm in src/Form/SettingsForm.php
Form constructor.

File

src/Form/SettingsForm.php, line 100

Class

SettingsForm
Defines an Instagram Feeds configuration form.

Namespace

Drupal\instagram_feeds\Form

Code

protected function buildMappingForm(array $form, FormStateInterface $form_state, $field_name) : array {
  $config = $this
    ->config(self::SETTINGS);
  $element = [
    '#title' => $this
      ->t('Fields Mapping'),
    '#type' => 'details',
    '#description' => $this
      ->t('Fields mapping between supported Media types and data from Instagram API.'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $media_types = $this
    ->getInstagramMediaTypes();

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');

  /** @var \Drupal\media\MediaTypeInterface $media_type */
  foreach ($media_types as $media_type_id => $media_type) {
    $element[$media_type_id] = [
      '#type' => 'details',
      '#title' => $media_type
        ->label(),
      '#open' => count($media_types) === 1,
      '#tree' => TRUE,
    ];
    $source_field_name = $media_type
      ->getSource()
      ->getSourceFieldDefinition($media_type)
      ->getName();
    $source_plugin = $media_type
      ->getSource()
      ->getPluginId();
    foreach ($entity_field_manager
      ->getFieldDefinitions('media', $media_type_id) as $field_definition) {
      if ($field_definition
        ->getName() == $source_field_name) {
        switch ($source_plugin) {
          case 'image':
            $element[$media_type_id][$field_definition
              ->getName()] = [
              '#type' => 'item',
              '#field_prefix' => $field_definition
                ->getLabel() . ': ',
              '#parents' => [],
              '#field_suffix' => $this
                ->getInstagramApiFields()['image'],
            ];
            break;
          case 'instagram':
            $element[$media_type_id][$field_definition
              ->getName()] = [
              '#type' => 'item',
              '#field_prefix' => $field_definition
                ->getLabel() . ': ',
              '#parents' => [],
              '#field_suffix' => $this
                ->getInstagramApiFields()['permalink'],
            ];
            break;
        }
      }
      elseif ($this
        ->isMappableFeild($field_definition)) {
        $element[$media_type_id][$field_definition
          ->getName()] = [
          '#type' => 'select',
          '#options' => $this
            ->getInstagramApiFields($field_definition
            ->getType()),
          '#empty_option' => $this
            ->t('- Skip -'),
          '#field_prefix' => $field_definition
            ->getLabel(),
          '#default_value' => $config
            ->get('mapping.' . $media_type_id . '.' . $field_definition
            ->getName()),
        ];
      }
      elseif ($field_definition
        ->getName() == 'name') {
        $element[$media_type_id]['name'] = [
          '#type' => 'textfield',
          '#field_prefix' => $this
            ->t('Media entity name'),
          '#required' => TRUE,
          '#default_value' => $config
            ->get('mapping.' . $media_type_id . '.name'),
        ] + $this
          ->getTokenDescription();
      }
    }
  }
  return $element;
}