You are here

class FieldBlockConfigForm in Field as Block 8.2

Configuration for select Entity types and delete blocks of unused types.

Hierarchy

Expanded class hierarchy of FieldBlockConfigForm

1 string reference to 'FieldBlockConfigForm'
fieldblock.routing.yml in ./fieldblock.routing.yml
fieldblock.routing.yml

File

src/Form/FieldBlockConfigForm.php, line 19

Namespace

Drupal\fieldblock\Form
View source
class FieldBlockConfigForm extends ConfigFormBase {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The block entity storage.
   *
   * @var \Drupal\fieldblock\BlockEntityStorage
   */
  protected $storage;

  /**
   * The field block controller.
   *
   * @var \Drupal\fieldblock\Controller\FieldBlockController
   */
  protected $fieldblockController;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The block manager.
   *
   * @var \Drupal\Core\Block\BlockManager
   */
  protected $blockManager;

  /**
   * Constructs a \Drupal\fieldblock\Form\FieldBlockConfigForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\fieldblock\BlockEntityStorage $storage
   *   The block entity storage.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   * @param \Drupal\Core\Block\BlockManager $blockManager
   *   The block manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entityTypeManager, BlockEntityStorage $storage, ModuleHandlerInterface $moduleHandler, BlockManager $blockManager) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entityTypeManager;
    $this->storage = $storage;
    $this->fieldblockController = new FieldBlockController();
    $this->moduleHandler = $moduleHandler;
    $this->blockManager = $blockManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.manager'), $container
      ->get('fieldblock.block_storage'), $container
      ->get('module_handler'), $container
      ->get('plugin.manager.block'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $enabled = $this->fieldblockController
      ->getEnabledEntityTypes();
    $form['enabled_entity_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Enable Entity Types'),
      '#options' => $this
        ->getEntityTypeLabels(),
      '#description' => $this
        ->t('Select the Entity Types to expose as field blocks.'),
      '#default_value' => $enabled,
    ];
    $orphaned_types = $this
      ->getOrphanedEntityTypes($enabled);
    $cleanup_options = [];
    $entity_type_definitions = $this->entityTypeManager
      ->getDefinitions();
    foreach ($orphaned_types as $entity_type) {
      if (isset($entity_type_definitions[$entity_type]) && $entity_type_definitions[$entity_type] instanceof ContentEntityTypeInterface) {

        // This entity type still exists on the site.
        $cleanup_options[$entity_type] = $entity_type_definitions[$entity_type]
          ->getLabel();
      }
      else {

        // This entity type no longer exists on the site.
        $cleanup_options[$entity_type] = $this
          ->t('Missing entity type: @type', [
          '@type' => $entity_type,
        ]);
      }
    }
    if (!empty($cleanup_options)) {
      $form['cleanup'] = [
        '#type' => 'checkboxes',
        '#required' => FALSE,
        '#title' => t('Clean up remaining field blocks of removed entity types'),
        '#description' => t('These entity types no longer exist, but one or more of their field blocks still do. Select the entity type(s) of which the field block(s) must be removed.'),
        '#default_value' => [],
        '#options' => $cleanup_options,
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Returns all entity types.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   Array of entity type definitions.
   */
  protected function getAllEntityTypes() {
    return array_keys($this->entityTypeManager
      ->getDefinitions());
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $clear_cache = FALSE;
    $previous_entity_types = $this->fieldblockController
      ->getEnabledEntityTypes();
    $new_entity_types = $form_state
      ->getValue('enabled_entity_types');
    if ($previous_entity_types != $new_entity_types) {
      $clear_cache = TRUE;
    }

    // Remove all blocks for entity types that are no longer enabled.
    if ($cleanup = $form_state
      ->getValue('cleanup')) {
      foreach ($cleanup as $entity_type => $value) {
        if ($value !== 0) {

          // Find and delete the remaining blocks for this entity type.
          $this->storage
            ->deleteBlocksForEntityType($entity_type);
          $clear_cache = TRUE;
          $this
            ->messenger()
            ->addStatus($this
            ->t('Remaining field blocks for the %type entity have been deleted.', [
            '%type' => $entity_type,
          ]));
        }
        else {
          if (in_array($entity_type, $this
            ->getAllEntityTypes())) {

            // Keep the entity type in the settings if it still exists.
            $new_entity_types[$entity_type] = $entity_type;
          }
        }
      }
    }
    $this
      ->config('fieldblock.settings')
      ->set('enabled_entity_types', $new_entity_types)
      ->save();
    if ($clear_cache) {

      // Invalidate the block cache to update fieldblock derivatives.
      if ($this->moduleHandler
        ->moduleExists('block')) {
        $this->blockManager
          ->clearCachedDefinitions();
      }
    }
  }

  /**
   * Get Entity Type labels for all compatible Entity Types.
   *
   * @return array
   *   Array of entity type labels keyed by the entity type machine name.
   */
  protected function getEntityTypeLabels() {
    $definitions = $this->entityTypeManager
      ->getDefinitions();
    $labels = [];

    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    foreach ($definitions as $definition) {
      if ($this->fieldblockController
        ->isFieldBlockCompatible($definition)) {
        $labels[$definition
          ->id()] = $definition
          ->getLabel();
      }
    }
    return $labels;
  }

  /**
   * Get orphaned entity types.
   *
   * Get all entity types that have Field Blocks but are either:
   *  1. No longer set to be used with this module
   *  2. Don't exist on the site.
   *
   * @param array $enabled_entity_types
   *   Currently enabled entity types.
   *
   * @return array
   *   Entity type ids.
   *
   * @todo param and return doc blocks must specify array of what, eg. string[].
   */
  protected function getOrphanedEntityTypes(array $enabled_entity_types) {
    $orphaned_types = [];
    $entity_types_used = $this->storage
      ->getEntityTypesUsed();
    $all_entity_types = $this
      ->getAllEntityTypes();
    foreach ($entity_types_used as $used_type) {
      if (!in_array($used_type, $all_entity_types) || !in_array($used_type, $enabled_entity_types)) {
        $orphaned_types[] = $used_type;
      }
    }
    return $orphaned_types;
  }

}

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
FieldBlockConfigForm::$blockManager protected property The block manager.
FieldBlockConfigForm::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
FieldBlockConfigForm::$fieldblockController protected property The field block controller.
FieldBlockConfigForm::$moduleHandler protected property The module handler.
FieldBlockConfigForm::$storage protected property The block entity storage.
FieldBlockConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FieldBlockConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
FieldBlockConfigForm::getAllEntityTypes protected function Returns all entity types.
FieldBlockConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FieldBlockConfigForm::getEntityTypeLabels protected function Get Entity Type labels for all compatible Entity Types.
FieldBlockConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FieldBlockConfigForm::getOrphanedEntityTypes protected function Get orphaned entity types.
FieldBlockConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FieldBlockConfigForm::__construct public function Constructs a \Drupal\fieldblock\Form\FieldBlockConfigForm object. Overrides ConfigFormBase::__construct
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.
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.