Skip to content

Commit

Permalink
Render app credential warnings with BigPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
mxr576 committed Oct 11, 2023
1 parent 90d4b38 commit a6c1b79
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 8 deletions.
16 changes: 16 additions & 0 deletions apigee_edge.install
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
40 changes: 32 additions & 8 deletions apigee_edge.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
],
];
}

Expand Down Expand Up @@ -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(),
]
];
}
}
Expand Down
1 change: 1 addition & 0 deletions config/install/apigee_edge.common_app_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions config/schema/apigee_edge.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/Form/AppSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
72 changes: 72 additions & 0 deletions src/LazyBuilder/AppWarningsLazyBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2023 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

namespace Drupal\apigee_edge\LazyBuilder;

use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\Core\Security\TrustedCallbackInterface;

/**
* Lazy builder for app credential warnings.
*/
final class AppWarningsLazyBuilder implements TrustedCallbackInterface {

/**
* Lazy builds app credentials warnings.
*
* @param string $entity_type_id
* The type of the entity, either "developer_app" or "team_app".
* @param string $id
* The entity id.
*
* @return array
* A render array with warnings.
*/
public static function lazyBuilder(string $entity_type_id, string $id): array {
if (!in_array($entity_type_id, ['developer_app', 'team_app'], TRUE)) {
throw new \LogicException(sprintf('Unexpected entity type: %s.', $entity_type_id));
}

$entity = \Drupal::entityTypeManager()->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'];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="app-credential-warnings">
<div class="app-credential-warning-placeholder">
<span>{{ 'Fetching crendential information...'|t }}</span>
</div>
</div>
5 changes: 5 additions & 0 deletions templates/apigee-app-credential-warnings.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="app-credential-warnings">
{% for warning in warnings %}
<div class="app-credential-warning">{{ warning }}</div>
{% endfor %}
</div>

0 comments on commit a6c1b79

Please sign in to comment.