You are here

public function LayoutBuilderRestrictionManager::getSortedPlugins in Layout Builder Restrictions 8.2

Helper function to get all restriction plugins.

Will get configuration for all plugins, regardless of being enabled or not, sorted by weight. For *only* enabled plugins, use getSortedEnabledPlugins().

Parameters

bool $get_disabled: Pass TRUE if the plugin data should also include plugins that are disabled.

Return value

array Returns the plugin information, with 'weight' and 'enabled' information. The array is sorted by the configured weight.

File

src/Plugin/LayoutBuilderRestrictionManager.php, line 61

Class

LayoutBuilderRestrictionManager
Provides the Layout builder restriction plugin plugin manager.

Namespace

Drupal\layout_builder_restrictions\Plugin

Code

public function getSortedPlugins(bool $get_disabled = FALSE) {
  $plugin_config = $this->configFactory
    ->get('layout_builder_restrictions.plugins')
    ->get('plugin_config');

  // Build a list of plugins, using saved weight & enabled status if defined.
  $plugin_list = [];
  foreach ($this
    ->getDefinitions() as $restriction_definition) {
    $id = $restriction_definition['id'];

    // Handle plugins that are in existing config.
    if ($plugin_config && !empty($plugin_config[$id])) {
      $config = $plugin_config[$id];
      if ($config['enabled'] == FALSE && $get_disabled) {
        $plugin_list[$id] = [
          'weight' => (int) $config['weight'],
          'enabled' => (bool) $config['enabled'],
          'title' => $restriction_definition['title'],
          'description' => $restriction_definition['description'],
        ];
      }
      elseif ($config['enabled'] == TRUE) {
        $plugin_list[$id] = [
          'weight' => (int) $config['weight'],
          'enabled' => (bool) $config['enabled'],
          'title' => $restriction_definition['title'],
          'description' => $restriction_definition['description'],
        ];
      }
    }
    else {

      // Plugin not in existing config, default to enabled & default weight.
      $plugin_list[$id] = [
        'weight' => 1,
        'enabled' => TRUE,
        'title' => $restriction_definition['title'],
        'description' => $restriction_definition['description'],
      ];
    }
  }

  // Sort the plugin list by weight.
  uasort($plugin_list, [
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightElement',
  ]);
  return $plugin_list;
}