You are here

class ViewUnpublishedInstallHelper in view_unpublished 8

Class ViewUnpublishedInstallHelper.

Hierarchy

Expanded class hierarchy of ViewUnpublishedInstallHelper

1 string reference to 'ViewUnpublishedInstallHelper'
view_unpublished.services.yml in ./view_unpublished.services.yml
view_unpublished.services.yml
1 service uses ViewUnpublishedInstallHelper
view_unpublished.install_helper in ./view_unpublished.services.yml
Drupal\view_unpublished\ViewUnpublishedInstallHelper

File

src/ViewUnpublishedInstallHelper.php, line 12

Namespace

Drupal\view_unpublished
View source
class ViewUnpublishedInstallHelper {

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Drupal\Core\Config\CachedStorage definition.
   *
   * @var \Drupal\Core\Config\CachedStorage
   */
  protected $configStorage;

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

  /**
   * Constructs a new ViewUnpublishedInstallHelper object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Config\CachedStorage $config_storage
   *   The config storage service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, CachedStorage $config_storage) {
    $this->entityTypeManager = $entity_type_manager;
    $this->configFactory = $config_factory;
    $this->configStorage = $config_storage;
  }

  /**
   * Helper that flags node_access to be rebuilt if unpublished nodes exist.
   */
  public function flagRebuild() {
    $query = $this->entityTypeManager
      ->getStorage('node')
      ->getQuery();
    $count_unpublished = (int) $query
      ->condition('status', FALSE)
      ->count()
      ->execute();
    if ($count_unpublished > 0) {
      node_access_needs_rebuild(TRUE);
    }
  }

  /**
   * Remove the errant view_unpublished dependency from Views.
   */
  public function removeDependency() {
    $view_names = $this->configStorage
      ->listAll('views.view');
    foreach ($view_names as $name) {
      $dependencies = $this->configFactory
        ->get($name)
        ->get('dependencies.module');
      if (!empty($dependencies) && array_key_exists('view_unpublished', array_flip($dependencies))) {
        $dependencies = array_diff($dependencies, [
          'view_unpublished',
        ]);
        $this->configFactory
          ->getEditable($name)
          ->set('dependencies.module', $dependencies)
          ->save(TRUE);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewUnpublishedInstallHelper::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
ViewUnpublishedInstallHelper::$configStorage protected property Drupal\Core\Config\CachedStorage definition.
ViewUnpublishedInstallHelper::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
ViewUnpublishedInstallHelper::flagRebuild public function Helper that flags node_access to be rebuilt if unpublished nodes exist.
ViewUnpublishedInstallHelper::removeDependency public function Remove the errant view_unpublished dependency from Views.
ViewUnpublishedInstallHelper::__construct public function Constructs a new ViewUnpublishedInstallHelper object.