You are here

public function FeedTypeForm::form in Feeds 8.3

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/FeedTypeForm.php, line 90

Class

FeedTypeForm
Form controller for the feed type edit forms.

Namespace

Drupal\feeds

Code

public function form(array $form, FormStateInterface $form_state) {
  $form['#tree'] = TRUE;
  $values = $form_state
    ->getValues();
  $form['#attached']['library'][] = 'feeds/feeds';
  $form['basics'] = [
    '#title' => $this
      ->t('Basic settings'),
    '#type' => 'details',
    '#open' => $this->entity
      ->isNew(),
    '#tree' => FALSE,
  ];
  $form['basics']['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#default_value' => $this->entity
      ->label(),
    '#maxlength' => '255',
    '#description' => $this
      ->t('A unique label for this feed type. This label will be displayed in the interface.'),
    '#required' => TRUE,
  ];
  $form['basics']['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $this->entity
      ->id(),
    '#disabled' => !$this->entity
      ->isNew(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#description' => $this
      ->t('A unique name for this feed type. It must only contain lowercase letters, numbers and underscores.'),
    '#machine_name' => [
      'exists' => 'Drupal\\feeds\\Entity\\FeedType::load',
      'source' => [
        'basics',
        'label',
      ],
    ],
    '#required' => TRUE,
  ];
  $form['basics']['description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('A description of this feed type.'),
    '#default_value' => $this->entity
      ->getDescription(),
  ];
  $form['basics']['help'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Explanation or submission guidelines'),
    '#default_value' => $this->entity
      ->getHelp(),
    '#description' => $this
      ->t('This text will be displayed at the top of the feed when creating or editing a feed of this type.'),
  ];
  $form['plugin_settings'] = [
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  ];
  $form['plugin_settings']['#prefix'] = '<div id="feeds-ajax-form-wrapper" class="feeds-feed-type-secondary-settings">';
  $form['plugin_settings']['#suffix'] = '</div>';
  $form['feed_type_settings'] = [
    '#type' => 'details',
    '#group' => 'plugin_settings',
    '#title' => $this
      ->t('Settings'),
    '#tree' => FALSE,
  ];
  $times = [
    900,
    1800,
    3600,
    10800,
    21600,
    43200,
    86400,
    259200,
    604800,
    2419200,
  ];
  $period = array_map(function ($time) {
    return $this->dateFormatter
      ->formatInterval($time);
  }, array_combine($times, $times));
  foreach ($period as &$p) {
    $p = $this
      ->t('Every @p', [
      '@p' => $p,
    ]);
  }
  $period = [
    FeedTypeInterface::SCHEDULE_NEVER => $this
      ->t('Off'),
    FeedTypeInterface::SCHEDULE_CONTINUOUSLY => $this
      ->t('As often as possible'),
  ] + $period;
  $cron_required = [
    '#type' => 'link',
    '#url' => Url::fromUri('https://www.drupal.org/docs/user_guide/en/security-cron.html'),
    '#title' => $this
      ->t('Requires cron to be configured.'),
    '#attributes' => [
      'target' => '_new',
    ],
  ];
  $form['feed_type_settings']['import_period'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Import period'),
    '#options' => $period,
    '#description' => $this
      ->t('Choose how often a feed should be imported.') . ' ' . $this->renderer
      ->renderRoot($cron_required),
    '#default_value' => $this->entity
      ->getImportPeriod(),
  ];
  foreach ($this->entity
    ->getPlugins() as $type => $plugin) {
    $options = $this->entity
      ->getPluginOptionsList($type);
    natcasesort($options);
    $form[$type . '_wrapper'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'feeds-plugin-inline',
        ],
      ],
    ];
    if (count($options) === 1) {
      $form[$type . '_wrapper']['id'] = [
        '#type' => 'value',
        '#value' => $plugin
          ->getPluginId(),
        '#plugin_type' => $type,
        '#parents' => [
          $type,
        ],
      ];
    }
    else {
      $form[$type . '_wrapper']['id'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('@type', [
          '@type' => ucfirst($type),
        ]),
        '#options' => $options,
        '#default_value' => $plugin
          ->getPluginId(),
        '#ajax' => [
          'callback' => '::ajaxCallback',
          'wrapper' => 'feeds-ajax-form-wrapper',
          'progress' => 'none',
        ],
        '#plugin_type' => $type,
        '#parents' => [
          $type,
        ],
      ];
    }

    // Give lockable plugins a chance to lock themselves.
    // @see \Drupal\feeds\Feeds\Processor\EntityProcessor::isLocked()
    if ($plugin instanceof LockableInterface) {
      $form[$type . '_wrapper']['id']['#disabled'] = $plugin
        ->isLocked();
    }
    $plugin_state = $this
      ->createSubFormState($type . '_configuration', $form_state);

    // This is the small form that appears under the select box.
    if ($this
      ->pluginHasForm($plugin, 'option')) {
      $option_form = $this->formFactory
        ->createInstance($plugin, 'option');
      $form[$type . '_wrapper']['advanced'] = $option_form
        ->buildConfigurationForm([], $plugin_state);
    }
    $form[$type . '_wrapper']['advanced']['#prefix'] = '<div id="feeds-plugin-' . $type . '-advanced">';
    $form[$type . '_wrapper']['advanced']['#suffix'] = '</div>';
    if ($this
      ->pluginHasForm($plugin, 'configuration')) {
      $form_builder = $this->formFactory
        ->createInstance($plugin, 'configuration');
      $plugin_form = $form_builder
        ->buildConfigurationForm([], $plugin_state);
      $form[$type . '_configuration'] = [
        '#type' => 'details',
        '#group' => 'plugin_settings',
        '#title' => $this
          ->t('@type settings', [
          '@type' => ucfirst($type),
        ]),
      ];
      $form[$type . '_configuration'] += $plugin_form;
    }
  }
  $form_state
    ->setValue($type . '_configuration', $plugin_state
    ->getValues());
  return parent::form($form, $form_state);
}