diff --git a/core/modules/settings_tray/src/Block/BlockEntityOffCanvasForm.php b/core/modules/settings_tray/src/Block/BlockEntityOffCanvasForm.php index 2c6f80d6e1..0deb09a843 100644 --- a/core/modules/settings_tray/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/settings_tray/src/Block/BlockEntityOffCanvasForm.php @@ -9,9 +9,16 @@ use Drupal\Core\Ajax\RedirectCommand; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Executable\ExecutableManagerInterface; +use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Plugin\Context\ContextRepositoryInterface; +use Drupal\Core\Plugin\PluginFormFactoryInterface; use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides form for block instance forms when used in the off-canvas dialog. @@ -22,6 +29,53 @@ * @internal */ class BlockEntityOffCanvasForm extends BlockForm { + /** + * @var \Drupal\Core\Config\ConfigFactoryOverrideInterface[] + */ + protected $configOverridServices; + + /** + * Constructs a BlockForm object. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Executable\ExecutableManagerInterface $manager + * The ConditionManager for building the visibility UI. + * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository + * The lazy context repository service. + * @param \Drupal\Core\Language\LanguageManagerInterface $language + * The language manager. + * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler + * The theme handler. + * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager + * The plugin form manager. + * @param \Drupal\Core\Config\ConfigFactoryOverrideInterface[] $config_override_services + * The service ids for config override services + */ + public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler, PluginFormFactoryInterface $plugin_form_manager, $config_override_services) { + parent::__construct($entity_manager, $manager, $context_repository, $language, $theme_handler, $plugin_form_manager); + $this->configOverridServices = $config_override_services; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + $service_ids = $container->getParameter('settings_tray.config_override_services'); + $override_services = []; + foreach ($service_ids as $service_id) { + $override_services[] = $container->get($service_id); + } + return new static( + $container->get('entity.manager'), + $container->get('plugin.manager.condition'), + $container->get('context.repository'), + $container->get('language_manager'), + $container->get('theme_handler'), + $container->get('plugin_form.factory'), + $override_services + ); + } /** * Provides a title callback to get the block's admin label. @@ -42,20 +96,27 @@ public function title(BlockInterface $block) { * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { - $form = parent::form($form, $form_state); - // Create link to full block form. $query = []; if ($destination = $this->getRequest()->query->get('destination')) { $query['destination'] = $destination; } - $form['advanced_link'] = [ + $advanced_link = [ '#type' => 'link', '#title' => $this->t('Advanced block options'), '#url' => $this->entity->toUrl('edit-form', ['query' => $query]), '#weight' => 1000, ]; + if ($this->hasOverrides()) { + + return [ + 'advanced_link' => $advanced_link, + ]; + } + $form = parent::form($form, $form_state); + + $form['advanced_link'] = $advanced_link; // Remove the ID and region elements. unset($form['id'], $form['region'], $form['settings']['admin_label']); @@ -191,4 +252,23 @@ protected function getRedirectUrl() { } } + /** + * Determines if the current block entity has any overrides. + * + * This finds any overrides at all regardless of whether they are currently + * in effect. + * + * @return bool + * TRUE if there are overrides, FALSE otherwise. + */ + protected function hasOverrides() { + foreach ($this->configOverridServices as $service) { + $overrides = $service->loadOverrides([$this->entity->getConfigDependencyName()]); + if ($overrides) { + return TRUE; + } + } + return FALSE; + } + } diff --git a/core/modules/settings_tray/src/SettingsTrayServiceProvider.php b/core/modules/settings_tray/src/SettingsTrayServiceProvider.php new file mode 100644 index 0000000000..01d07d653b --- /dev/null +++ b/core/modules/settings_tray/src/SettingsTrayServiceProvider.php @@ -0,0 +1,21 @@ +findTaggedServiceIds('config.factory.override')); + $container->setParameter('settings_tray.config_override_services', $service_ids); + } + +}