UwDashboardRestrictBlocksLayouts.php 3.61 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\uw_dashboard\Plugin\LayoutBuilderRestriction;

5
use Drupal\Core\Config\ConfigFactoryInterface;
6
use Drupal\layout_builder_restrictions\Plugin\LayoutBuilderRestriction\EntityViewModeRestriction;
7 8 9
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Liam Morland's avatar
Liam Morland committed
10
 * Class for restricting blocks/layouts for UW dashboard.
11 12
 *
 * @LayoutBuilderRestriction(
13 14 15
 *   id = "uw_dashboard_restrict_blocks_layouts",
 *   title = @Translation("UW Dashboard restrict blocks/layouts"),
 *   description = @Translation("Restrict blocks/layouts for UW dashboard"),
16 17
 * )
 */
18
class UwDashboardRestrictBlocksLayouts extends EntityViewModeRestriction {
19

Liam Morland's avatar
Liam Morland committed
20 21 22 23 24
  /**
   * Config factory to load blocks allowed to be displayed on dashboard.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
25
  protected $configFactory;
26 27

  /**
Liam Morland's avatar
Liam Morland committed
28
   * {@inheritdoc}
29
   */
Igor Biki's avatar
Igor Biki committed
30 31 32 33 34 35
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
36 37 38 39
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
40
      $container->get('config.factory')
41 42 43 44 45 46 47 48 49 50 51 52
    );
  }

  /**
   * Constructs a Drupal\Component\Plugin\PluginBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
53 54
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Config factory to load blocks allowed to be displayed on dashboard.
55
   */
Igor Biki's avatar
Igor Biki committed
56 57 58 59 60 61
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    ConfigFactoryInterface $configFactory
  ) {
62 63 64
    $this->configuration = $configuration;
    $this->pluginId = $plugin_id;
    $this->pluginDefinition = $plugin_definition;
65
    $this->configFactory = $configFactory;
66 67 68
  }

  /**
Liam Morland's avatar
Liam Morland committed
69
   * {@inheritdoc}
70 71
   */
  public function alterBlockDefinitions(array $definitions, array $context) {
72

73
    // Load configuration for allowed blocks.
Igor Biki's avatar
Igor Biki committed
74 75
    $allowed_blocks = $this->configFactory->get('uw_dashboard.allowed_blocks')
      ->get('allowed_blocks');
76 77
    $allowed_block_definitions = [];

78 79
    // Filter blocks only for dashboard override, while for admin dashboard
    // layout page will display all available blocks.
80
    if ($allowed_blocks && isset($context['section_storage']) && $context['section_storage']->getPluginId() === 'dashboards_override') {
Igor Biki's avatar
Igor Biki committed
81 82 83
      foreach ($allowed_blocks as $key => $allowed_block) {
        if (isset($definitions[$key])) {
          $allowed_block_definitions[$key] = $definitions[$key];
84 85 86
        }
      }
    }
87

88
    return $allowed_block_definitions ?: $definitions;
89
  }
Igor Biki's avatar
Igor Biki committed
90

91 92 93 94 95 96 97 98 99
  /**
   * {@inheritdoc}
   */
  public function alterSectionDefinitions(array $definitions, array $context) {

    // If we are on the dashboards, restrict the layouts.
    if ($context['section_storage']->pluginId == 'dashboards' || $context['section_storage']->pluginId == 'dashboards_override') {

      // The allowed layouts machine names.
Liam Morland's avatar
Liam Morland committed
100 101 102 103 104 105 106
      $allowed_layouts = [
        'uw_1_column',
        'uw_2_column',
        'uw_3_column',
        'uw_inverted_l_left',
        'uw_inverted_l_right',
      ];
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

      // Step through each of the definitions and unset anything
      // that is not in the allowed layouts array.
      foreach ($definitions as $key => $definition) {

        // If it is not in the allowed layout array unset the layout.
        if (!in_array($key, $allowed_layouts)) {

          // Unset the layout.
          unset($definitions[$key]);
        }
      }
    }

    return $definitions;
  }
Liam Morland's avatar
Liam Morland committed
123

124
}