Skip to content

Commit b93acb2

Browse files
committed
Render app credential warnings with BigPipe
1 parent 90d4b38 commit b93acb2

File tree

8 files changed

+146
-8
lines changed

8 files changed

+146
-8
lines changed

apigee_edge.install

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ function apigee_edge_requirements($phase) {
102102
// Do nothing.
103103
}
104104
}
105+
106+
if (\Drupal::configFactory()->getEditable('apigee_edge.common_app_settings')->get('app_credential_warnings_bc_mode')) {
107+
$requirements['apigee_edge_app_credential_warnings_bc_mode'] = [
108+
'title' => t('Apigee Edge'),
109+
'description' => t('App credential warnings are rendered in BC mode but that is going to removed in Apigee Edge 4.0.0.'),
110+
'severity' => REQUIREMENT_WARNING,
111+
];
112+
}
105113
}
106114

107115
return $requirements;
@@ -389,3 +397,11 @@ function apigee_edge_update_9002() {
389397
$edge_settings['content']['callbackUrl'] = $new_edge_settings['content']['callbackUrl'];
390398
$config_storage->write('core.entity_view_display.developer_app.developer_app.default', $edge_settings);
391399
}
400+
401+
/**
402+
* Enable BC rendering mode for app credential warnings.
403+
*/
404+
function apigee_edge_update_10301(): void {
405+
$app_settings = \Drupal::configFactory()->getEditable('apigee_edge.common_app_settings');
406+
$app_settings->set('app_credential_warnings_bc_mode', TRUE)->save(TRUE);
407+
}

apigee_edge.module

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use Drupal\Component\Utility\Crypt;
4343
use Drupal\Component\Utility\Xss;
4444
use Drupal\Core\Access\AccessResult;
4545
use Drupal\Core\Breadcrumb\Breadcrumb;
46+
use Drupal\Core\Cache\CacheableMetadata;
4647
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
4748
use Drupal\Core\Entity\EntityInterface;
4849
use Drupal\Core\Entity\EntityTypeInterface;
@@ -138,6 +139,12 @@ function apigee_edge_theme() {
138139
'render element' => 'elements',
139140
'base hook' => 'apigee_secret',
140141
],
142+
'apigee_app_credential_warnings' => [
143+
'variables' => ['warnings' => []],
144+
],
145+
'apigee_app_credential_warnings_placeholder' => [
146+
'variables' => ['app_type' => NULL, 'id' => NULL],
147+
],
141148
];
142149
}
143150

@@ -450,17 +457,38 @@ function apigee_edge_entity_view(array &$build, EntityInterface $entity, EntityV
450457
}
451458

452459
if ($display->getComponent('warnings')) {
453-
/** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */
454-
$app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
455-
$warnings = array_filter($app_warnings_checker->getWarnings($entity));
456-
if (count($warnings)) {
460+
$app_settings_config = \Drupal::configFactory()->getEditable('apigee_edge.common_app_settings');
461+
CacheableMetadata::createFromRenderArray($build)->addCacheableDependency($app_settings_config)->applyTo($build);
462+
463+
464+
if ($app_settings_config->get('app_credential_warnings_bc_mode')) {
465+
/** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */
466+
$app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
467+
$warnings = array_filter($app_warnings_checker->getWarnings($entity));
468+
if (count($warnings)) {
469+
$build['warnings'] = [
470+
'#theme' => 'status_messages',
471+
'#message_list' => [
472+
'warning' => $warnings,
473+
],
474+
];
475+
}
476+
}
477+
else {
457478
$build['warnings'] = [
458-
'#theme' => 'status_messages',
459-
'#message_list' => [
460-
'warning' => $warnings,
461-
],
479+
'#lazy_builder' => ['\Drupal\apigee_edge\LazyBuilder\AppWarningsLazyBuilder::lazyBuilder', [$entity->getEntityType()->id(), $entity->id()]],
480+
'#create_placeholder' => TRUE,
481+
'#lazy_builder_preview' => [
482+
'#theme' => 'apigee_app_credential_warnings_placeholder',
483+
'#app_type' => $entity->getEntityType()->id(),
484+
'#id' => $entity->id(),
485+
]
462486
];
463487
}
488+
489+
// if (array_key_exists('warnings', $build)) {
490+
// CacheableMetadata::createFromRenderArray($build['warnings'])->addCacheableDependency($app_settings_config)->applyTo($build['warnings']);
491+
// }
464492
}
465493
}
466494

config/install/apigee_edge.common_app_settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ callback_url_pattern: '^https?:\/\/.*$'
1010
callback_url_pattern_error_message: 'The Callback URL must start with http:// or https://'
1111
callback_url_description: 'External site to which a consumer of this app is redirected to log in when using three-legged OAuth.'
1212
callback_url_placeholder: ''
13+
app_credential_warnings_bc_mode: false

config/schema/apigee_edge.schema.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ apigee_edge.common_app_settings:
6666
callback_url_placeholder:
6767
type: label
6868
label: 'Placeholder for a Callback URL'
69+
app_credential_warnings_bc_mode:
70+
type: boolean
71+
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."
6972

7073
apigee_edge.developer_app_settings:
7174
type: config_object

src/Form/AppSettingsForm.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
173173
'#required' => $form_state->getValue('user_select') === NULL ? !(bool) $common_app_settings->get('user_select') : !(bool) $form_state->getValue('user_select'),
174174
];
175175

176+
$form['app_credential_warnings_bc_mode'] = [
177+
'#type' => 'checkbox',
178+
'#title' => $this->t('Render app credential warnings synchronously'),
179+
'#description' => $this->t('Whether to render app credentials synchronously or asynchronously with BigPipe to reduce page load time.'),
180+
'#default_value' => $common_app_settings->get('app_credential_warnings_bc_mode'),
181+
];
182+
176183
return parent::buildForm($form, $form_state);
177184
}
178185

@@ -209,6 +216,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
209216
->set('display_as_select', $form_state->getValue('display_as_select'))
210217
->set('user_select', $form_state->getValue('user_select'))
211218
->set('default_products', array_values(array_filter($form_state->getValue('default_api_product_multiple'))))
219+
->set('app_credential_warnings_bc_mode', $form_state->getValue('app_credential_warnings_bc_mode'))
212220
->save();
213221

214222
parent::submitForm($form, $form_state);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/**
6+
* Copyright 2023 Google Inc.
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* version 2 as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20+
* MA 02110-1301, USA.
21+
*/
22+
23+
namespace Drupal\apigee_edge\LazyBuilder;
24+
25+
use Drupal\apigee_edge\Entity\AppInterface;
26+
use Drupal\Core\Security\TrustedCallbackInterface;
27+
28+
/**
29+
* Lazy builder for app credential warnings.
30+
*/
31+
final class AppWarningsLazyBuilder implements TrustedCallbackInterface {
32+
33+
/**
34+
* Lazy builds app credentials warnings.
35+
*
36+
* @param string $entity_type_id
37+
* The type of the entity, either "developer_app" or "team_app".
38+
* @param string $id
39+
* The entity id.
40+
*
41+
* @return array
42+
* A render array with warnings.
43+
*/
44+
public static function lazyBuilder(string $entity_type_id, string $id): array {
45+
if (!in_array($entity_type_id, ['developer_app', 'team_app'], TRUE)) {
46+
throw new \LogicException(sprintf('Unexpected entity type: %s.', $entity_type_id));
47+
}
48+
49+
$entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->load($id);
50+
if ($entity instanceof AppInterface) {
51+
/** @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker */
52+
$app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
53+
$warnings = array_filter($app_warnings_checker->getWarnings($entity));
54+
if (count($warnings) > 0) {
55+
return [
56+
'#theme' => 'apigee_app_credential_warnings',
57+
'#warnings' => $warnings,
58+
];
59+
}
60+
}
61+
62+
return [];
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public static function trustedCallbacks(): array {
69+
return ['lazyBuilder'];
70+
}
71+
72+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="app-credential-warnings">
2+
<div class="app-credential-warning-placeholder">
3+
<span>{{ 'Fetching crendential information...'|t }}</span>
4+
</div>
5+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="app-credential-warnings">
2+
{% for warning in warnings %}
3+
<div class="app-credential-warning">{{ warning }}</div>
4+
{% endfor %}
5+
</div>

0 commit comments

Comments
 (0)