From a6c1b79ac6becfa26bb0bdc610e0eb7823f0af43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Wed, 11 Oct 2023 15:58:27 +0200 Subject: [PATCH] Render app credential warnings with BigPipe --- apigee_edge.install | 16 +++++ apigee_edge.module | 40 ++++++++--- .../apigee_edge.common_app_settings.yml | 1 + config/schema/apigee_edge.schema.yml | 3 + src/Form/AppSettingsForm.php | 8 +++ src/LazyBuilder/AppWarningsLazyBuilder.php | 72 +++++++++++++++++++ ...-credential-warnings-placeholder.html.twig | 5 ++ .../apigee-app-credential-warnings.html.twig | 5 ++ 8 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 src/LazyBuilder/AppWarningsLazyBuilder.php create mode 100644 templates/apigee-app-credential-warnings-placeholder.html.twig create mode 100644 templates/apigee-app-credential-warnings.html.twig diff --git a/apigee_edge.install b/apigee_edge.install index 941bb79c..61514ee8 100644 --- a/apigee_edge.install +++ b/apigee_edge.install @@ -102,6 +102,14 @@ function apigee_edge_requirements($phase) { // Do nothing. } } + + if (\Drupal::configFactory()->getEditable('apigee_edge.common_app_settings')->get('app_credential_warnings_bc_mode')) { + $requirements['apigee_edge_app_credential_warnings_bc_mode'] = [ + 'title' => t('Apigee Edge'), + 'description' => t('App credential warnings are rendered in BC mode but that is going to removed in Apigee Edge 4.0.0.'), + 'severity' => REQUIREMENT_WARNING, + ]; + } } return $requirements; @@ -389,3 +397,11 @@ function apigee_edge_update_9002() { $edge_settings['content']['callbackUrl'] = $new_edge_settings['content']['callbackUrl']; $config_storage->write('core.entity_view_display.developer_app.developer_app.default', $edge_settings); } + +/** + * Enable BC rendering mode for app credential warnings. + */ +function apigee_edge_update_10301(): void { + $app_settings = \Drupal::configFactory()->getEditable('apigee_edge.common_app_settings'); + $app_settings->set('app_credential_warnings_bc_mode', TRUE)->save(TRUE); +} diff --git a/apigee_edge.module b/apigee_edge.module index ad7685de..2d890219 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -43,6 +43,7 @@ use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Xss; use Drupal\Core\Access\AccessResult; use Drupal\Core\Breadcrumb\Breadcrumb; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; @@ -138,6 +139,12 @@ function apigee_edge_theme() { 'render element' => 'elements', 'base hook' => 'apigee_secret', ], + 'apigee_app_credential_warnings' => [ + 'variables' => ['warnings' => []], + ], + 'apigee_app_credential_warnings_placeholder' => [ + 'variables' => ['app_type' => NULL, 'id' => NULL], + ], ]; } @@ -450,15 +457,32 @@ function apigee_edge_entity_view(array &$build, EntityInterface $entity, EntityV } if ($display->getComponent('warnings')) { - /** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */ - $app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker'); - $warnings = array_filter($app_warnings_checker->getWarnings($entity)); - if (count($warnings)) { + $app_settings_config = \Drupal::configFactory()->getEditable('apigee_edge.common_app_settings'); + CacheableMetadata::createFromRenderArray($build)->addCacheableDependency($app_settings_config)->applyTo($build); + + + if ($app_settings_config->get('app_credential_warnings_bc_mode')) { + /** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */ + $app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker'); + $warnings = array_filter($app_warnings_checker->getWarnings($entity)); + if (count($warnings)) { + $build['warnings'] = [ + '#theme' => 'status_messages', + '#message_list' => [ + 'warning' => $warnings, + ], + ]; + } + } + else { $build['warnings'] = [ - '#theme' => 'status_messages', - '#message_list' => [ - 'warning' => $warnings, - ], + '#lazy_builder' => ['\Drupal\apigee_edge\LazyBuilder\AppWarningsLazyBuilder::lazyBuilder', [$entity->getEntityType()->id(), $entity->id()]], + '#create_placeholder' => TRUE, + '#lazy_builder_preview' => [ + '#theme' => 'apigee_app_credential_warnings_placeholder', + '#app_type' => $entity->getEntityType()->id(), + '#id' => $entity->id(), + ] ]; } } diff --git a/config/install/apigee_edge.common_app_settings.yml b/config/install/apigee_edge.common_app_settings.yml index 509cf7ac..b71b18ae 100644 --- a/config/install/apigee_edge.common_app_settings.yml +++ b/config/install/apigee_edge.common_app_settings.yml @@ -10,3 +10,4 @@ callback_url_pattern: '^https?:\/\/.*$' callback_url_pattern_error_message: 'The Callback URL must start with http:// or https://' callback_url_description: 'External site to which a consumer of this app is redirected to log in when using three-legged OAuth.' callback_url_placeholder: '' +app_credential_warnings_bc_mode: false diff --git a/config/schema/apigee_edge.schema.yml b/config/schema/apigee_edge.schema.yml index 78c92a01..afbe6bfa 100644 --- a/config/schema/apigee_edge.schema.yml +++ b/config/schema/apigee_edge.schema.yml @@ -66,6 +66,9 @@ apigee_edge.common_app_settings: callback_url_placeholder: type: label label: 'Placeholder for a Callback URL' + app_credential_warnings_bc_mode: + type: boolean + deprecated: "The 'app_credential_warnings_bc_mode' config schema is deprecated in Apigee Edge 3.0.3 and will be removed from Apigee Edge 4.0.0." apigee_edge.developer_app_settings: type: config_object diff --git a/src/Form/AppSettingsForm.php b/src/Form/AppSettingsForm.php index 7a094917..defdedd0 100644 --- a/src/Form/AppSettingsForm.php +++ b/src/Form/AppSettingsForm.php @@ -173,6 +173,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => $form_state->getValue('user_select') === NULL ? !(bool) $common_app_settings->get('user_select') : !(bool) $form_state->getValue('user_select'), ]; + $form['app_credential_warnings_bc_mode'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Render app credential warnings synchronously'), + '#description' => $this->t('Whether to render app credentials synchronously or asynchronously with BigPipe to reduce page load time.'), + '#default_value' => $common_app_settings->get('app_credential_warnings_bc_mode'), + ]; + return parent::buildForm($form, $form_state); } @@ -209,6 +216,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set('display_as_select', $form_state->getValue('display_as_select')) ->set('user_select', $form_state->getValue('user_select')) ->set('default_products', array_values(array_filter($form_state->getValue('default_api_product_multiple')))) + ->set('app_credential_warnings_bc_mode', $form_state->getValue('app_credential_warnings_bc_mode')) ->save(); parent::submitForm($form, $form_state); diff --git a/src/LazyBuilder/AppWarningsLazyBuilder.php b/src/LazyBuilder/AppWarningsLazyBuilder.php new file mode 100644 index 00000000..d4c00d5e --- /dev/null +++ b/src/LazyBuilder/AppWarningsLazyBuilder.php @@ -0,0 +1,72 @@ +getStorage($entity_type_id)->load($id); + if ($entity instanceof AppInterface) { + /** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */ + $app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker'); + $warnings = array_filter($app_warnings_checker->getWarnings($entity)); + if (count($warnings) > 0) { + return [ + '#theme' => 'apigee_app_credential_warnings', + '#warnings' => $warnings, + ]; + } + } + + return []; + } + + /** + * {@inheritdoc} + */ + public static function trustedCallbacks(): array { + return ['lazyBuilder']; + } + +} diff --git a/templates/apigee-app-credential-warnings-placeholder.html.twig b/templates/apigee-app-credential-warnings-placeholder.html.twig new file mode 100644 index 00000000..6c4804ee --- /dev/null +++ b/templates/apigee-app-credential-warnings-placeholder.html.twig @@ -0,0 +1,5 @@ +
+
+ {{ 'Fetching crendential information...'|t }} +
+
diff --git a/templates/apigee-app-credential-warnings.html.twig b/templates/apigee-app-credential-warnings.html.twig new file mode 100644 index 00000000..7a798c0e --- /dev/null +++ b/templates/apigee-app-credential-warnings.html.twig @@ -0,0 +1,5 @@ +
+ {% for warning in warnings %} +
{{ warning }}
+ {% endfor %} +