You are here

public function DefaultEntityProcessorForm::buildConfigurationForm in Feeds 8.3

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Feeds/Processor/Form/DefaultEntityProcessorForm.php, line 49

Class

DefaultEntityProcessorForm
The configuration form for the CSV parser.

Namespace

Drupal\feeds\Feeds\Processor\Form

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $tokens = [
    '@entity' => mb_strtolower($this->plugin
      ->entityTypeLabel()),
    '@entities' => mb_strtolower($this->plugin
      ->entityTypeLabelPlural()),
  ];
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->plugin
    ->entityType());
  if ($entity_type
    ->getKey('langcode')) {
    $langcode = $this->plugin
      ->getConfiguration('langcode');
    $form['langcode'] = [
      '#type' => 'select',
      '#options' => $this->plugin
        ->languageOptions(),
      '#title' => $this
        ->t('Language'),
      '#required' => TRUE,
      '#default_value' => $langcode,
    ];

    // Add default value as one of the options if not yet available.
    if ($langcode && !isset($form['langcode']['#options'][$langcode])) {
      $form['langcode']['#options'][$langcode] = $this
        ->t('Unknown language: @language', [
        '@language' => $langcode,
      ]);
    }
  }
  $form['insert_new'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Insert new @entities', $tokens),
    '#description' => $this
      ->t('New @entities will be determined using mappings that are a "unique target".', $tokens),
    '#options' => [
      ProcessorInterface::INSERT_NEW => $this
        ->t('Insert new @entities', $tokens),
      ProcessorInterface::SKIP_NEW => $this
        ->t('Do not insert new @entities', $tokens),
    ],
    '#default_value' => $this->plugin
      ->getConfiguration('insert_new'),
  ];
  $form['update_existing'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Update existing @entities', $tokens),
    '#description' => $this
      ->t('Existing @entities will be determined using mappings that are <strong>unique</strong>.', $tokens),
    '#options' => [
      ProcessorInterface::SKIP_EXISTING => $this
        ->t('Do not update existing @entities', $tokens),
      ProcessorInterface::REPLACE_EXISTING => $this
        ->t('Replace existing @entities', $tokens),
      ProcessorInterface::UPDATE_EXISTING => $this
        ->t('Update existing @entities', $tokens),
    ],
    '#default_value' => $this->plugin
      ->getConfiguration('update_existing'),
  ];
  $times = [
    ProcessorInterface::EXPIRE_NEVER,
    3600,
    10800,
    21600,
    43200,
    86400,
    259200,
    604800,
    2592000,
    2592000 * 3,
    2592000 * 6,
    31536000,
  ];
  $period = array_map([
    $this,
    'formatExpire',
  ], array_combine($times, $times));
  $options = $this
    ->getUpdateNonExistentActions();
  $selected = $this->plugin
    ->getConfiguration('update_non_existent');
  if (!isset($options[$selected])) {
    $options[$selected] = $this
      ->t('@label (action no longer available)', [
      '@label' => $selected,
    ]);
  }
  if (!empty($options)) {
    $form['update_non_existent'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Previously imported items'),
      '#description' => $this
        ->t('Select what to do with items that were previously imported, but are now no longer in the feed.'),
      '#options' => $options,
      '#default_value' => $this->plugin
        ->getConfiguration('update_non_existent'),
    ];
  }
  $form['expire'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Expire @entities', $tokens),
    '#options' => $period,
    '#description' => $this
      ->t('Select after how much time @entities should be deleted.', $tokens),
    '#default_value' => $this->plugin
      ->getConfiguration('expire'),
  ];
  if ($entity_type
    ->entityClassImplements(EntityOwnerInterface::class)) {
    $form['owner_feed_author'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Owner: Feed author'),
      '#description' => $this
        ->t('Use the feed author as the owner of the entities to be created.'),
      '#default_value' => $this->plugin
        ->getConfiguration('owner_feed_author'),
    ];
    $form['owner_id'] = [
      '#type' => 'entity_autocomplete',
      '#title' => $this
        ->t('Owner'),
      '#description' => $this
        ->t('Select the owner of the entities to be created. Leave blank for %anonymous.', [
        '%anonymous' => \Drupal::config('user.settings')
          ->get('anonymous'),
      ]),
      '#target_type' => 'user',
      '#default_value' => User::load($this->plugin
        ->getConfiguration('owner_id')),
      '#states' => [
        'invisible' => [
          'input[name="processor_configuration[owner_feed_author]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  $form['advanced'] = [
    '#title' => $this
      ->t('Advanced settings'),
    '#type' => 'details',
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
    '#weight' => 10,
  ];
  if ($entity_type
    ->entityClassImplements(EntityOwnerInterface::class)) {
    $form['advanced']['authorize'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Authorize'),
      '#description' => $this
        ->t('Check that the author has permission to create the @entity.', $tokens),
      '#default_value' => $this->plugin
        ->getConfiguration('authorize'),
      '#parents' => [
        'processor_configuration',
        'authorize',
      ],
    ];
  }
  $form['advanced']['skip_hash_check'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force update'),
    '#description' => $this
      ->t('Forces the update of items even if the feed did not change.'),
    '#default_value' => $this->plugin
      ->getConfiguration('skip_hash_check'),
    '#parents' => [
      'processor_configuration',
      'skip_hash_check',
    ],
    '#states' => [
      'visible' => [
        'input[name="processor_configuration[update_existing]"]' => [
          'value' => ProcessorInterface::UPDATE_EXISTING,
        ],
      ],
    ],
  ];
  return $form;
}