You are here

protected function LayoutBuilderRestrictionsByRegionHelperTrait::regionRestrictionStatus in Layout Builder Restrictions 8.2

Checks if any restrictions are enabled for a given region.

Either $static_id or $entity_view_display_id is required.

Parameters

string $layout_plugin: The machine name of the layout plugin.

string $region_id: The machine name of the region.

mixed $static_id: (optional) A unique string representing a built form; optionally NULL.

mixed $entity_view_display_id: (optional) The ID of the entity view display; optionally NULL.

Return value

bool A boolean indicating whether or not a region has restrictions.

1 call to LayoutBuilderRestrictionsByRegionHelperTrait::regionRestrictionStatus()
LayoutBuilderRestrictionsByRegionHelperTrait::regionRestrictionStatusString in modules/layout_builder_restrictions_by_region/src/Traits/LayoutBuilderRestrictionsByRegionHelperTrait.php
Wrapper function for regionRestrictionStatus() that returns a string.

File

modules/layout_builder_restrictions_by_region/src/Traits/LayoutBuilderRestrictionsByRegionHelperTrait.php, line 27

Class

LayoutBuilderRestrictionsByRegionHelperTrait
Methods to help Layout Builder Restrictions By Region plugin.

Namespace

Drupal\layout_builder_restrictions_by_region\Traits

Code

protected function regionRestrictionStatus(string $layout_plugin, string $region_id, $static_id = NULL, $entity_view_display_id = NULL) {
  if (is_null($static_id) && is_null($entity_view_display_id)) {
    throw new Exception("Either a static ID or a entity view display ID must be provided.");
  }
  $region_categories = NULL;
  $region_restricted = FALSE;

  // Attempt to retrieve config from tempstore.
  $tempstore = $this
    ->privateTempStoreFactory();
  $store = $tempstore
    ->get('layout_builder_restrictions_by_region');

  // If tempstore return is null, then no record is found.
  // If tempstore returns something other than null, then a record is found.
  // If tempstore returns an empty array, then a record is found with
  // no restrictions.
  $region_categories = $store
    ->get($static_id . ':' . $layout_plugin . ':' . $region_id);
  if (!is_null($region_categories)) {
    $region_restricted = empty($region_categories) ? FALSE : TRUE;
  }
  else {
    $display = $this
      ->entityTypeManager()
      ->getStorage('entity_view_display')
      ->load($entity_view_display_id);
    $third_party_settings = $display
      ->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction_by_region', []);
    if (isset($third_party_settings['restricted_categories'][$layout_plugin][$region_id])) {
      return TRUE;
    }
    if (isset($third_party_settings['whitelisted_blocks'][$layout_plugin][$region_id])) {
      return TRUE;
    }
    elseif (isset($third_party_settings['blacklisted_blocks'][$layout_plugin][$region_id])) {
      if (!empty($third_party_settings['blacklisted_blocks'][$layout_plugin][$region_id])) {
        return TRUE;
      }
      else {
        return FALSE;
      }
    }
    return FALSE;
  }
  return $region_restricted;
}