You are here

abstract class AssignmentFormBase in Features 8.3

Same name and namespace in other branches
  1. 8.4 modules/features_ui/src/Form/AssignmentFormBase.php \Drupal\features_ui\Form\AssignmentFormBase

Configures the selected configuration assignment method for this site.

Hierarchy

Expanded class hierarchy of AssignmentFormBase

File

modules/features_ui/src/Form/AssignmentFormBase.php, line 16

Namespace

Drupal\features_ui\Form
View source
abstract class AssignmentFormBase extends FormBase {

  /**
   * The features manager.
   *
   * @var \Drupal\features\FeaturesManagerInterface
   */
  protected $featuresManager;

  /**
   * The package assigner.
   *
   * @var \Drupal\features\FeaturesAssignerInterface
   */
  protected $assigner;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The current bundle.
   *
   * @var \Drupal\features\FeaturesBundleInterface
   */
  protected $currentBundle;

  /**
   * Constructs a AssignmentBaseForm object.
   *
   * @param \Drupal\features\FeaturesManagerInterface $features_manager
   *   The features manager.
   * @param \Drupal\features\FeaturesAssignerInterface $assigner
   *   The assigner.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(FeaturesManagerInterface $features_manager, FeaturesAssignerInterface $assigner, EntityTypeManagerInterface $entity_type_manager) {
    $this->featuresManager = $features_manager;
    $this->assigner = $assigner;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('features.manager'), $container
      ->get('features_assigner'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Adds configuration types checkboxes.
   */
  protected function setConfigTypeSelect(&$form, $defaults, $type, $bundles_only = FALSE, $description = '') {
    $options = $this->featuresManager
      ->listConfigTypes($bundles_only);
    if (!isset($form['types'])) {
      $form['types'] = [
        '#type' => 'container',
        '#tree' => TRUE,
      ];
    }
    $form['types']['config'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Configuration types'),
      '#description' => !empty($description) ? $description : $this
        ->t('Select types of configuration that should be considered @type types.', [
        '@type' => $type,
      ]),
      '#options' => $options,
      '#default_value' => $defaults,
    ];
  }

  /**
   * Adds content entity types checkboxes.
   */
  protected function setContentTypeSelect(&$form, $defaults, $type, $exclude_has_config_bundles = TRUE) {
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $has_config_bundle = [];
    foreach ($entity_types as $definition) {
      if ($entity_type_id = $definition
        ->getBundleOf()) {
        $has_config_bundle[] = $entity_type_id;
      }
    }
    $options = [];
    foreach ($entity_types as $entity_type_id => $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface) {
        continue;
      }
      if ($exclude_has_config_bundles && in_array($entity_type_id, $has_config_bundle)) {
        continue;
      }
      $options[$entity_type_id] = $entity_type
        ->getLabel() ?: $entity_type_id;
    }

    // Sort the entity types by label.
    uasort($options, 'strnatcasecmp');
    if (!isset($form['types'])) {
      $form['types'] = [
        '#type' => 'container',
        '#tree' => TRUE,
      ];
    }
    $form['types']['content'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Content entity types'),
      '#description' => $this
        ->t('Select content entity types that should be considered @type types.', [
        '@type' => $type,
      ]),
      '#options' => $options,
      '#default_value' => $defaults,
    ];
  }

  /**
   * Adds a "Save settings" submit action.
   */
  protected function setActions(&$form, $method_id = NULL) {
    $assignment_info = $this->assigner
      ->getAssignmentMethods();
    if (isset($method_id) && isset($assignment_info[$method_id])) {
      $method = $assignment_info[$method_id];
      $form['help_text'] = [
        '#markup' => $method['description'],
        '#prefix' => '<p class="messages messages--status">',
        '#suffix' => '</p>',
        '#weight' => -99,
      ];
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Save settings'),
    ];
    $form['#attributes']['class'][] = 'features-assignment-settings-form';
    $form['#attached'] = [
      'library' => [
        'features_ui/drupal.features_ui.admin',
      ],
    ];
  }

  /**
   * Redirects back to the Bundle config form.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  protected function setRedirect(FormStateInterface $form_state) {
    $form_state
      ->setRedirect('features.assignment', [
      'bundle_name' => $this->currentBundle
        ->getMachineName(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssignmentFormBase::$assigner protected property The package assigner.
AssignmentFormBase::$currentBundle protected property The current bundle.
AssignmentFormBase::$entityTypeManager protected property The entity type manager.
AssignmentFormBase::$featuresManager protected property The features manager.
AssignmentFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 1
AssignmentFormBase::setActions protected function Adds a "Save settings" submit action.
AssignmentFormBase::setConfigTypeSelect protected function Adds configuration types checkboxes.
AssignmentFormBase::setContentTypeSelect protected function Adds content entity types checkboxes.
AssignmentFormBase::setRedirect protected function Redirects back to the Bundle config form.
AssignmentFormBase::__construct public function Constructs a AssignmentBaseForm object. 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
FormInterface::buildForm public function Form constructor. 179
FormInterface::getFormId public function Returns a unique string identifying the form. 236
FormInterface::submitForm public function Form submission handler. 192
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.