You are here

public function FeaturesExportForm::buildForm in Features 8.4

Same name and namespace in other branches
  1. 8.3 modules/features_ui/src/Form/FeaturesExportForm.php \Drupal\features_ui\Form\FeaturesExportForm::buildForm()

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/features_ui/src/Form/FeaturesExportForm.php, line 115

Class

FeaturesExportForm
Defines the configuration export form.

Namespace

Drupal\features_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();

  // TODO: See if there is a Drupal Core issue for this.
  // Sometimes the first ajax call on the page causes buildForm to be called
  // twice!  First time form_state->getTriggeringElement is NOT SET, but
  // the form_state['input'] shows the _triggering_element_name.  Then the
  // SECOND time it is called the getTriggeringElement is fine.
  $real_trigger = $this
    ->elementTriggeredScriptedSubmission($form_state);
  if (!isset($trigger) && $real_trigger == 'bundle') {
    $input = $form_state
      ->getUserInput();
    $bundle_name = $input['bundle'];
    $this->assigner
      ->setCurrent($this->assigner
      ->getBundle($bundle_name));
  }
  elseif (isset($trigger['#name']) && $trigger['#name'] == 'bundle') {
    $bundle_name = $form_state
      ->getValue('bundle', '');
    $this->assigner
      ->setCurrent($this->assigner
      ->getBundle($bundle_name));
  }
  else {
    $this->assigner
      ->loadBundle();
  }
  $current_bundle = $this->assigner
    ->getBundle();
  $this->assigner
    ->assignConfigPackages();
  $packages = $this->featuresManager
    ->getPackages();
  $config_collection = $this->featuresManager
    ->getConfigCollection();

  // Add in un-packaged configuration items.
  $this
    ->addUnpackaged($packages, $config_collection);
  $packages = $this->featuresManager
    ->filterPackages($packages, $current_bundle
    ->getMachineName());

  // Pass the packages and bundle data for use in the form pre_render
  // callback.
  $form['#packages'] = $packages;
  $form['#profile_package'] = $current_bundle
    ->getProfileName();
  $form['header'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'features-header',
    ],
  ];
  $bundle_options = $this->assigner
    ->getBundleOptions();

  // If there are no custom bundles, provide message.
  if (count($bundle_options) < 2) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('You have not yet created any bundles. Before generating features, you may wish to <a href=":create">create a bundle</a> to group your features within.', [
      ':create' => Url::fromRoute('features.assignment')
        ->toString(),
    ]));
  }
  $form['#prefix'] = '<div id="edit-features-wrapper">';
  $form['#suffix'] = '</div>';
  $form['header']['bundle'] = [
    '#title' => $this
      ->t('Bundle'),
    '#type' => 'select',
    '#options' => $bundle_options,
    '#default_value' => $current_bundle
      ->getMachineName(),
    '#prefix' => '<div id="edit-package-set-wrapper">',
    '#suffix' => '</div>',
    '#ajax' => [
      'callback' => '::updatePreview',
      'wrapper' => 'edit-features-preview-wrapper',
    ],
    '#attributes' => [
      'data-new-package-set' => 'status',
    ],
  ];
  $form['preview'] = $this
    ->buildListing($packages, $current_bundle);
  $form['#attached'] = [
    'library' => [
      'features_ui/drupal.features_ui.admin',
    ],
  ];
  if (\Drupal::currentUser()
    ->hasPermission('export configuration')) {

    // Offer available generation methods.
    $generation_info = $this->generator
      ->getGenerationMethods();

    // Sort generation methods by weight.
    uasort($generation_info, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
    $form['description'] = [
      '#markup' => '<p>' . $this
        ->t('Use an export method button below to generate the selected features.') . '</p>',
    ];
    $form['actions'] = [
      '#type' => 'actions',
      '#tree' => TRUE,
    ];
    foreach ($generation_info as $method_id => $method) {
      $form['actions'][$method_id] = [
        '#type' => 'submit',
        '#name' => $method_id,
        '#value' => $this
          ->t('@name', [
          '@name' => $method['name'],
        ]),
        '#attributes' => [
          'title' => Html::escape($method['description']),
        ],
      ];
    }
  }
  $form['#pre_render'][] = [
    get_class($this),
    'preRenderRemoveInvalidCheckboxes',
  ];
  return $form;
}