You are here

public function FeaturesDiffForm::buildForm in Features 8.4

Same name and namespace in other branches
  1. 8.3 modules/features_ui/src/Form/FeaturesDiffForm.php \Drupal\features_ui\Form\FeaturesDiffForm::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/FeaturesDiffForm.php, line 105

Class

FeaturesDiffForm
Defines the features differences form.

Namespace

Drupal\features_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $featurename = '') {
  $current_bundle = $this->assigner
    ->applyBundle();
  $packages = $this->featuresManager
    ->getPackages();
  $form = [];
  $machine_name = '';
  if (!empty($featurename) && empty($packages[$featurename])) {
    $this
      ->messenger()
      ->addError($this
      ->t('Feature @name does not exist.', [
      '@name' => $featurename,
    ]));
    return [];
  }
  elseif (!empty($featurename)) {
    $machine_name = $packages[$featurename]
      ->getMachineName();
    $packages = [
      $packages[$featurename],
    ];
  }
  else {
    $packages = $this->featuresManager
      ->filterPackages($packages, $current_bundle
      ->getMachineName());
  }
  $header = [
    'row' => [
      'data' => !empty($machine_name) ? $this
        ->t('Differences in @name', [
        '@name' => $machine_name,
      ]) : ($current_bundle
        ->isDefault() ? $this
        ->t('All differences') : $this
        ->t('All differences in bundle: @bundle', [
        '@bundle' => $current_bundle
          ->getName(),
      ])),
    ],
  ];
  $options = [];
  foreach ($packages as $package) {
    if ($package
      ->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT) {
      $missing = $this->featuresManager
        ->reorderMissing($this->featuresManager
        ->detectMissing($package));
      $overrides = $this->featuresManager
        ->detectOverrides($package, TRUE);
      if (!empty($overrides) || !empty($missing)) {
        $options += [
          $package
            ->getMachineName() => [
            'row' => [
              'data' => [
                '#type' => 'html_tag',
                '#tag' => 'h2',
                '#value' => Html::escape($package
                  ->getName()),
              ],
            ],
            '#attributes' => [
              'class' => 'features-diff-header',
            ],
          ],
        ];
        $options += $this
          ->diffOutput($package, $overrides, $missing);
      }
    }
  }
  $form['diff'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#attributes' => [
      'class' => [
        'features-diff-listing',
      ],
    ],
    '#empty' => $this
      ->t('No differences exist in exported features.'),
  ];
  $form['actions'] = [
    '#type' => 'actions',
    '#tree' => TRUE,
  ];
  $form['actions']['revert'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import changes'),
  ];
  $form['actions']['help'] = [
    '#markup' => $this
      ->t('Import the selected changes above into the active configuration.'),
  ];
  $form['#attached']['library'][] = 'system/diff';
  $form['#attached']['library'][] = 'features_ui/drupal.features_ui.admin';
  return $form;
}