Commit cca568b2 authored by Tim Bozeman's avatar Tim Bozeman Committed by Lucas Hedding
Browse files

Issue #3259011: Add Component Overrides

parent f87d49fb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,4 +7,5 @@ php: 7.4
dependencies:
  - entity:entity
  - codemirror_editor:codemirror_editor
  - drupal:layout_builder
configure: entity.component_library_pattern.collection
+5 −0
Original line number Diff line number Diff line
@@ -14,3 +14,8 @@ pattern_listing:
  dependencies:
    - core/drupal
    - core/once

component_override:
  css:
    component:
      css/component-override.css: {}
+7 −0
Original line number Diff line number Diff line
@@ -9,3 +9,10 @@ entity.component_library_variant.add_form:
  route_name: entity.component_library_variant.add_form
  appears_on:
    - entity.component_library_variant.collection

entity.component_override.add_form:
  title: Add component override
  route_name: entity.component_override.add_form
  appears_on:
    - entity.component_override.collection
    - system.theme_settings_theme
+49 −1
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
use Drupal\component_library\Entity\ComponentLibraryPattern;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme().
@@ -29,6 +30,25 @@ function component_library_theme(): array {
  ];
}

/**
 * Implements hook_theme_registry_alter().
 */
function component_library_theme_registry_alter(&$theme_registry): void {
  // Ensure there is a theme registry entry for each override.
  $theme = Drupal::theme()->getActiveTheme();
  if ($theme) {
    $theme_name = $theme->getName();
    $overrides = \Drupal::entityTypeManager()->getStorage('component_override')->loadByProperties(['theme' => $theme_name]);
    if ($overrides) {
      $override_manager = Drupal::service('plugin.manager.component_override');
      foreach ($overrides as $override) {
        /** @var \Drupal\component_library\ComponentOverrideManager $override_manager */
        $override_manager->ensureThemeRegistry($override, $theme_registry);
      }
    }
  }
}

function template_preprocess_html__component_library__variant_preview(&$variables): void {
  template_preprocess_html($variables);
}
@@ -80,7 +100,7 @@ function template_preprocess_component_library_variant(array &$variables): void
  $component_library_variant = $variables['elements']['#component_library_variant'];
  if ($pattern_entity = $component_library_variant->getPatternEntity()) {
    try {
      $context = Yaml::decode($pattern_entity->get('data')) ?: [];
      $context = Yaml::decode($pattern_entity->get('data')) ?? [];
    }
    catch (InvalidDataTypeException $exception) {
      // Intentionally empty.
@@ -109,3 +129,31 @@ function component_library_codemirror_editor_assets_alter(array &$assets): void
  $assets['css'][] = 'addon/hint/show-hint.css ';
  $assets['css'][] = 'addon/lint/lint.css';
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Adds list of theme's component_override entities to the theme settings.
 */
function component_library_form_system_theme_settings_alter(&$form, FormStateInterface $form_state): void {
  $theme = $form_state->getBuildInfo()['args'][0];
  if ($theme) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = 'component_override';
    $overrides = $entity_type_manager->getStorage($entity_type)
      ->loadByProperties(['theme' => $theme]);

    if ($overrides) {
      $form['component_overrides'] = [
        '#type' => 'details',
        '#title' => t('Component overrides'),
        '#open' => TRUE,
        '#weight' => -50,
      ];

      $form['component_overrides']['list'] = $entity_type_manager
        ->getListBuilder($entity_type)
        ->render();
    }
  }
}
+36 −0
Original line number Diff line number Diff line
@@ -12,3 +12,39 @@ services:
    tags:
      # It needs to be loaded before @twig.loader.string.
      - {name: twig.loader, priority: -150}
  component_library.entity_field_yaml_placeholder.event_subscriber:
    class: Drupal\component_library\EventSubscriber\EntityFieldYamlPlaceholders
    tags:
      - { name: event_subscriber }
  plugin.manager.component_override:
    class: Drupal\component_library\ComponentOverrideManager
    parent: default_plugin_manager
    arguments:
      - '@theme.registry'
      - '@extension.list.theme'
  component_library:not_admin:
    class: Drupal\component_library\Routing\NotAdminRouteSubscriber
    tags:
      - { name: event_subscriber }
  component_library.twig.loader:
    class: Drupal\component_library\Twig\Loader
    arguments:
      - '@entity_type.manager'
      - '@theme.manager'
    tags:
      - { name: twig.loader, priority: 110 }
  component_library.prepare_override.content_entity.variables:
    class: Drupal\component_library\EventSubscriber\OverrideContentEntityVariables
    arguments: ['@layout_builder.sample_entity_generator', '@plugin.manager.component_override', '@entity_type.manager']
    tags:
      - { name: event_subscriber }
  component_library.prepare_override.preprocess:
    class: Drupal\component_library\EventSubscriber\OverridePreprocess
    arguments: ['@plugin.manager.component_override']
    tags:
      - { name: event_subscriber }
  component_library.prepare_override.content_entity.theme_suggestions:
    class: Drupal\component_library\EventSubscriber\OverrideContentEntityThemeSuggestions
    arguments: ['@module_handler']
    tags:
      - { name: event_subscriber }
Loading