You are here

class SmartTitleConfigForm in Smart Title 8

SmartTitleConfigForm.

Hierarchy

Expanded class hierarchy of SmartTitleConfigForm

1 string reference to 'SmartTitleConfigForm'
smart_title_ui.routing.yml in modules/smart_title_ui/smart_title_ui.routing.yml
modules/smart_title_ui/smart_title_ui.routing.yml

File

modules/smart_title_ui/src/Form/SmartTitleConfigForm.php, line 21

Namespace

Drupal\smart_title_ui\Form
View source
class SmartTitleConfigForm extends ConfigFormBase {

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

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Constructs a new UpdateSettingsForm.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bunde_info
   *   The entity type bundle info service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bunde_info) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->entityTypeBundleInfo = $entity_type_bunde_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'smart_title.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'smart_title_config_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('smart_title.settings');
    $entity_type_definitions = $this->entityTypeManager
      ->getDefinitions();

    // Collecting content entity types which have canonical link template.
    $content_entity_type_filter = function (EntityTypeInterface $entity_type_definition) {
      return $entity_type_definition instanceof ContentEntityTypeInterface && $entity_type_definition
        ->entityClassImplements(FieldableEntityInterface::class) && $entity_type_definition
        ->get('field_ui_base_route');
    };
    $valid_content_entity_types = array_filter($entity_type_definitions, $content_entity_type_filter);
    $entity_bundles = [];
    foreach (array_keys($valid_content_entity_types) as $entity_type_id) {
      $label_key = $valid_content_entity_types[$entity_type_id]
        ->getKey('label');
      if ($label_key) {
        $base_field_definitions = $this->entityFieldManager
          ->getBaseFieldDefinitions($entity_type_id);
        if (!$base_field_definitions[$label_key]
          ->isDisplayConfigurable('view')) {
          $entity_bundles[$entity_type_id]['label'] = $valid_content_entity_types[$entity_type_id]
            ->getLabel();
          $entity_bundles[$entity_type_id]['bundles'] = $this->entityTypeBundleInfo
            ->getBundleInfo($entity_type_id);
        }
      }
    }
    $defaults = $config
      ->get('smart_title') ?: [];
    foreach ($entity_bundles as $type => $definitions) {
      $options = [];
      $default = [];
      foreach ($definitions['bundles'] as $key => $info) {
        $options["{$type}:{$key}"] = $info['label'];
        if (in_array("{$type}:{$key}", $defaults)) {
          $default["{$type}:{$key}"] = "{$type}:{$key}";
        }
      }
      $form[$type . '_bundles'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Smart Title for @entity-type', [
          '@entity-type' => $definitions['label'],
        ]),
        '#options' => $options,
        '#default_value' => $default,
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $values = $form_state
      ->getValues();
    $smart_title_bundles_setting = $smart_title_bundles = [];
    foreach ($values as $key => $bundle_values) {
      if (strpos($key, '_bundles')) {
        foreach ($bundle_values as $bundle_key => $bundle_value) {
          if ($bundle_value) {
            $smart_title_bundles_setting[] = $bundle_key;
          }
          $smart_title_bundles[] = $bundle_key;
        }
      }
    }

    // Updating entity view displays:
    // Remove smart title where it's not available anymore.
    $evd_storage = $this->entityTypeManager
      ->getStorage('entity_view_display');
    $evds = $evd_storage
      ->loadMultiple();
    $not_smart_title_capable_bundles = array_diff($smart_title_bundles, $smart_title_bundles_setting);
    foreach ($evds as $evd_id => $evd) {
      assert($evd instanceof EntityViewDisplayInterface);
      list($target_entity_type_id, $target_bundle) = explode('.', $evd_id);
      if (in_array("{$target_entity_type_id}:{$target_bundle}", $not_smart_title_capable_bundles)) {
        $evd
          ->unsetThirdPartySetting('smart_title', 'enabled')
          ->unsetThirdPartySetting('smart_title', 'settings')
          ->save();
      }
    }
    $this
      ->config('smart_title.settings')
      ->set('smart_title', $smart_title_bundles_setting)
      ->save();
    Cache::invalidateTags([
      'entity_field_info',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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::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
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.
SmartTitleConfigForm::$entityFieldManager protected property The entity field manager.
SmartTitleConfigForm::$entityTypeBundleInfo protected property The entity type bundle info service.
SmartTitleConfigForm::$entityTypeManager protected property The entity type manager.
SmartTitleConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SmartTitleConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SmartTitleConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SmartTitleConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SmartTitleConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SmartTitleConfigForm::__construct public function Constructs a new UpdateSettingsForm. Overrides ConfigFormBase::__construct
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.