View source
<?php
namespace Drupal\features_ui\Form;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\features\FeaturesAssignerInterface;
use Drupal\features\FeaturesGeneratorInterface;
use Drupal\features\FeaturesManagerInterface;
use Drupal\features\FeaturesBundleInterface;
use Drupal\features\Package;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeaturesExportForm extends FormBase implements TrustedCallbackInterface {
protected $featuresManager;
protected $assigner;
protected $generator;
protected $moduleHandler;
public function __construct(FeaturesManagerInterface $features_manager, FeaturesAssignerInterface $assigner, FeaturesGeneratorInterface $generator, ModuleHandlerInterface $module_handler) {
$this->featuresManager = $features_manager;
$this->assigner = $assigner;
$this->generator = $generator;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('features.manager'), $container
->get('features_assigner'), $container
->get('features_generator'), $container
->get('module_handler'));
}
public static function trustedCallbacks() {
return [
'preRenderRemoveInvalidCheckboxes',
];
}
public function getFormId() {
return 'features_export_form';
}
protected function elementTriggeredScriptedSubmission(FormStateInterface &$form_state) {
$input = $form_state
->getUserInput();
if (!empty($input['_triggering_element_name'])) {
return $input['_triggering_element_name'];
}
return '';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$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();
$this
->addUnpackaged($packages, $config_collection);
$packages = $this->featuresManager
->filterPackages($packages, $current_bundle
->getMachineName());
$form['#packages'] = $packages;
$form['#profile_package'] = $current_bundle
->getProfileName();
$form['header'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'features-header',
],
];
$bundle_options = $this->assigner
->getBundleOptions();
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')) {
$generation_info = $this->generator
->getGenerationMethods();
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;
}
public function updatePreview($form, FormStateInterface $form_state) {
$form = $this
->preRenderRemoveInvalidCheckboxes($form);
return $form['preview'];
}
protected function buildListing(array $packages, FeaturesBundleInterface $bundle) {
$header = [
'name' => [
'data' => $this
->t('Feature'),
],
'machine_name' => [
'data' => $this
->t(''),
],
'details' => [
'data' => $this
->t('Description'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'version' => [
'data' => $this
->t('Version'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'status' => [
'data' => $this
->t('Status'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'state' => [
'data' => $this
->t('State'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
];
$options = [];
$first = TRUE;
foreach ($packages as $package) {
if ($first && $package
->getStatus() == FeaturesManagerInterface::STATUS_NO_EXPORT) {
if ($package
->getStatus() === FeaturesManagerInterface::STATUS_NO_EXPORT && !$bundle
->isProfilePackage($package
->getMachineName()) && empty($package
->getConfig())) {
continue;
}
$first = FALSE;
$options[] = [
'name' => [
'data' => $this
->t('The following packages are not exported.'),
'class' => 'features-export-header-row',
'colspan' => 6,
],
];
}
$options[$package
->getMachineName()] = $this
->buildPackageDetail($package, $bundle);
}
$element = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#attributes' => [
'class' => [
'features-listing',
],
],
'#prefix' => '<div id="edit-features-preview-wrapper">',
'#suffix' => '</div>',
];
return $element;
}
protected function buildPackageDetail(Package $package, FeaturesBundleInterface $bundle) {
$config_collection = $this->featuresManager
->getConfigCollection();
$url = Url::fromRoute('features.edit', [
'featurename' => $package
->getMachineName(),
]);
$element['name'] = [
'data' => \Drupal::service('link_generator')
->generate($package
->getName(), $url),
'class' => [
'feature-name',
],
];
$machine_name = $package
->getMachineName();
if ($machine_name !== 'unpackaged') {
$machine_name = $bundle
->getFullName($machine_name);
}
$element['machine_name'] = $machine_name;
$element['status'] = [
'data' => $this->featuresManager
->statusLabel($package
->getStatus()),
'class' => [
'column-nowrap',
],
];
$element['version'] = [
'data' => Html::escape($package
->getVersion()),
'class' => [
'column-nowrap',
],
];
$overrides = $this->featuresManager
->detectOverrides($package);
$new_config = $this->featuresManager
->detectNew($package);
$conflicts = [];
$missing = [];
$moved = [];
if ($package
->getStatus() == FeaturesManagerInterface::STATUS_NO_EXPORT) {
$overrides = [];
$new_config = [];
}
$package_config = [];
foreach ($package
->getConfig() as $item_name) {
if (isset($config_collection[$item_name])) {
$item = $config_collection[$item_name];
$package_config[$item
->getType()][] = [
'name' => Html::escape($item_name),
'label' => Html::escape($item
->getLabel()),
'class' => in_array($item_name, $overrides) ? 'features-override' : (in_array($item_name, $new_config) ? 'features-detected' : ''),
];
}
}
foreach ($package
->getConfigOrig() as $item_name) {
if (!isset($config_collection[$item_name])) {
$missing[] = $item_name;
$package_config['missing'][] = [
'name' => Html::escape($item_name),
'label' => Html::escape($item_name),
'class' => 'features-missing',
];
}
elseif (!in_array($item_name, $package
->getConfig())) {
$item = $config_collection[$item_name];
if (empty($item
->getProvider())) {
$conflicts[] = $item_name;
$package_name = !empty($item
->getPackage()) ? $item
->getPackage() : $this
->t('PACKAGE NOT ASSIGNED');
$package_config[$item
->getType()][] = [
'name' => Html::escape($package_name),
'label' => Html::escape($item
->getLabel()),
'class' => 'features-conflict',
];
}
else {
$moved[] = $item_name;
$package_name = !empty($item
->getPackage()) ? $item
->getPackage() : $this
->t('PACKAGE NOT ASSIGNED');
$package_config[$item
->getType()][] = [
'name' => $this
->t('Moved to @package', [
'@package' => $package_name,
]),
'label' => Html::escape($item
->getLabel()),
'class' => 'features-moved',
];
}
}
}
$package_config['dependencies'] = [];
foreach ($package
->getDependencies() as $dependency) {
$package_config['dependencies'][] = [
'name' => $dependency,
'label' => $this->moduleHandler
->moduleExists($dependency) ? $this->moduleHandler
->getName($dependency) : $dependency,
'class' => '',
];
}
$class = '';
$state_links = [];
if (!empty($conflicts)) {
$state_links[] = [
'#type' => 'link',
'#title' => $this
->t('Conflicts'),
'#url' => Url::fromRoute('features.edit', [
'featurename' => $package
->getMachineName(),
]),
'#attributes' => [
'class' => [
'features-conflict',
],
],
];
}
if (!empty($overrides)) {
$state_links[] = [
'#type' => 'link',
'#title' => $this->featuresManager
->stateLabel(FeaturesManagerInterface::STATE_OVERRIDDEN),
'#url' => Url::fromRoute('features.diff', [
'featurename' => $package
->getMachineName(),
]),
'#attributes' => [
'class' => [
'features-override',
],
],
];
}
if (!empty($new_config)) {
$state_links[] = [
'#type' => 'link',
'#title' => $this
->t('New detected'),
'#url' => Url::fromRoute('features.diff', [
'featurename' => $package
->getMachineName(),
]),
'#attributes' => [
'class' => [
'features-detected',
],
],
];
}
if (!empty($missing) && $package
->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED) {
$state_links[] = [
'#type' => 'link',
'#title' => $this
->t('Missing'),
'#url' => Url::fromRoute('features.edit', [
'featurename' => $package
->getMachineName(),
]),
'#attributes' => [
'class' => [
'features-missing',
],
],
];
}
if (!empty($moved)) {
$state_links[] = [
'#type' => 'link',
'#title' => $this
->t('Moved'),
'#url' => Url::fromRoute('features.edit', [
'featurename' => $package
->getMachineName(),
]),
'#attributes' => [
'class' => [
'features-moved',
],
],
];
}
if (!empty($state_links)) {
$element['state'] = [
'data' => $state_links,
'class' => [
'column-nowrap',
],
];
}
else {
$element['state'] = '';
}
$config_types = $this->featuresManager
->listConfigTypes();
$config_types['dependencies'] = $this
->t('Dependencies');
$config_types['missing'] = $this
->t('Missing');
uasort($config_types, 'strnatcasecmp');
$rows = [];
foreach ($config_types as $type => $label) {
$row = [];
if (isset($package_config[$type])) {
$row[] = [
'data' => [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => Html::escape($label),
'#attributes' => [
'title' => Html::escape($type),
'class' => 'features-item-label',
],
],
];
$row[] = [
'data' => [
'#theme' => 'features_items',
'#items' => $package_config[$type],
'#value' => Html::escape($label),
'#title' => Html::escape($type),
],
'class' => 'item',
];
$rows[] = $row;
}
}
$element['table'] = [
'#type' => 'table',
'#rows' => $rows,
];
$details = [];
$details['description'] = [
'#markup' => Xss::filterAdmin($package
->getDescription()),
];
$details['table'] = [
'#type' => 'details',
'#title' => $this
->t('Included configuration'),
'#description' => [
'data' => $element['table'],
],
];
$element['details'] = [
'class' => [
'description',
'expand',
],
'data' => $details,
];
return $element;
}
protected function addUnpackaged(array &$packages, array $config_collection) {
$packages['unpackaged'] = new Package('unpackaged', [
'name' => $this
->t('Unpackaged'),
'description' => $this
->t('Configuration that has not been added to any package.'),
'config' => [],
'status' => FeaturesManagerInterface::STATUS_NO_EXPORT,
'version' => '',
]);
foreach ($config_collection as $item_name => $item) {
if (!$item
->getPackage() && !$item
->isExcluded() && !$item
->isProviderExcluded()) {
$packages['unpackaged']
->appendConfig($item_name);
}
}
}
public static function preRenderRemoveInvalidCheckboxes(array $form) {
foreach ($form['#packages'] as $package) {
if (empty($package
->getConfig()) && !($package
->getMachineName() == $form['#profile_package']) || $package
->getMachineName() == 'unpackaged') {
$form['preview'][$package
->getMachineName()]['#access'] = FALSE;
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_bundle = $this->assigner
->loadBundle();
$this->assigner
->assignConfigPackages();
$package_names = array_filter($form_state
->getValue('preview'));
if (empty($package_names)) {
$this
->messenger()
->addWarning($this
->t('Please select one or more packages to export.'));
return;
}
$method_id = NULL;
$trigger = $form_state
->getTriggeringElement();
$op = $form_state
->getValue('op');
if (!empty($trigger) && empty($op)) {
$method_id = $trigger['#name'];
}
if (!empty($method_id)) {
$this->generator
->generatePackages($method_id, $current_bundle, $package_names);
$this->generator
->applyExportFormSubmit($method_id, $form, $form_state);
}
}
}