From 88bd7c25358a0567ed389466f3af9485a5e80f50 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 18 Apr 2019 21:26:15 +0400 Subject: [PATCH 01/28] wip for DRUP-728 --- apigee_devportal_kickstart.info.yml | 1 + apigee_devportal_kickstart.install | 43 +- apigee_devportal_kickstart.routing.yml | 7 + apigee_devportal_kickstart.services.yml | 3 + composer.json | 2 + .../apigee_kickstart_m10n.info.yml | 7 + .../apigee_m10n_add_credit.config.yml | 2 + ...er.commerce_order_item_type.add_credit.yml | 9 + ...oduct.commerce_product_type.add_credit.yml | 17 + ...erce_product_variation_type.add_credit.yml | 9 + ...erce_order_item.add_credit.add_to_cart.yml | 36 ++ ...ay.commerce_product.add_credit.default.yml | 79 +++ ...e_product_variation.add_credit.default.yml | 39 ++ ...ay.commerce_product.add_credit.default.yml | 44 ++ ...e_product_variation.add_credit.default.yml | 17 + ...field.commerce_product.add_credit.body.yml | 21 + ...eld.commerce_product.add_credit.stores.yml | 20 + ...commerce_product.add_credit.variations.yml | 23 + src/ApigeeDevportalKickstartManager.php | 68 +++ ...geeDevportalKickstartConfigurationForm.php | 527 ++++++++++++++++++ .../Form/ApigeeEdgeConfigurationForm.php | 8 + 21 files changed, 973 insertions(+), 9 deletions(-) create mode 100644 apigee_devportal_kickstart.routing.yml create mode 100644 apigee_devportal_kickstart.services.yml create mode 100644 modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml create mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml create mode 100644 src/ApigeeDevportalKickstartManager.php create mode 100644 src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php diff --git a/apigee_devportal_kickstart.info.yml b/apigee_devportal_kickstart.info.yml index 724e3f7f..c9b73a2f 100644 --- a/apigee_devportal_kickstart.info.yml +++ b/apigee_devportal_kickstart.info.yml @@ -56,6 +56,7 @@ install: - admin_toolbar_links_access_filter - admin_toolbar_tools - adminimal_admin_toolbar +- address - apigee_edge - apigee_edge_apidocs - apigee_kickstart_customizer diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index afe6aab6..b5b6d093 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -24,6 +24,8 @@ */ use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; +use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartConfigurationForm; +use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; use Drupal\Core\Messenger\MessengerInterface; /** @@ -83,26 +85,29 @@ function apigee_devportal_kickstart_install() { * Implements hook_install_tasks_alter(). */ function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) { - // Add a task for configuring Apigee Edge Authentication. - $tasks_copy = $tasks; - $apigee_edge_configure_form = [ - 'apigee_edge_configure_form' => [ + // Add tasks for configuring Apigee authentication and monetization. + $apigee_kickstart_tasks = [ + ApigeeEdgeConfigurationForm::class => [ 'display_name' => t('Configure Apigee Edge'), 'type' => 'form', - 'function' => ApigeeEdgeConfigurationForm::class, ], ]; // The task should run before install_configure_form which creates the user. - $tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_edge_configure_form + $tasks_copy; + $tasks_copy = $tasks; + $tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_kickstart_tasks + $tasks_copy; } /** * Implements hook_install_tasks(). */ function apigee_devportal_kickstart_install_tasks(&$install_state) { - $tasks = [ - '\Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm' => [ + return [ + ApigeeDevportalKickstartConfigurationForm::class => [ + 'display_name' => t('Configure kickstart'), + 'type' => 'form', + ], + DemoInstallForm::class => [ 'display_name' => t('Install demo content'), 'type' => 'form', ], @@ -111,7 +116,6 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { 'display' => FALSE, ], ]; - return $tasks; } /** @@ -140,3 +144,24 @@ function apigee_devportal_kickstart_theme_setup(array &$install_state) { ->set('use_admin_theme', TRUE) ->save(TRUE); } + +//function apigee_devportal_kickstart_isMonetizationEnabled() { +// if (Drupal::moduleHandler()->moduleExists('apigee_edge')) { +// try { +// $active_key_id = Drupal::configFactory() +// ->get(AuthenticationForm::CONFIG_NAME) +// ->get('active_key'); +// if ($active_key = Drupal::service('key.repository') +// ->getKey($active_key_id)) { +// $sdk_connector = \Drupal::service('apigee_edge.sdk_connector'); +// $org_controller = new OrganizationController($sdk_connector->getClient()); +// $org = $org_controller->load($sdk_connector->getOrganization()); +// return $org->getPropertyValue('features.isMonetizationEnabled') === 'true'; +// } +// } catch (\Exception $e) { +// return FALSE; +// } +// } +// +// return FALSE; +//} diff --git a/apigee_devportal_kickstart.routing.yml b/apigee_devportal_kickstart.routing.yml new file mode 100644 index 00000000..33cafd33 --- /dev/null +++ b/apigee_devportal_kickstart.routing.yml @@ -0,0 +1,7 @@ +kickstart.debug: + path: '/admin/config/m10n' + defaults: + _form: '\Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeM10nConfigurationForm' + _title: 'Configure m10n' + requirements: + _permission: 'administer site' diff --git a/apigee_devportal_kickstart.services.yml b/apigee_devportal_kickstart.services.yml new file mode 100644 index 00000000..1daa811f --- /dev/null +++ b/apigee_devportal_kickstart.services.yml @@ -0,0 +1,3 @@ +services: + apigee_devportal_kickstart.manager: + class: Drupal\apigee_devportal_kickstart\ApigeeDevportalKickstartManager diff --git a/composer.json b/composer.json index 6e9152af..96188d36 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,9 @@ "drupal/admin_toolbar": "^1.0", "drupal/adminimal_admin_toolbar": "^1.9", "drupal/apigee_edge": "1.x-dev", + "drupal/apigee_m10n": "1.x-dev", "drupal/better_exposed_filters": "^3.0@alpha", + "drupal/commerce": "^2.13", "drupal/core": "^8.6.0", "drupal/default_content": "^1.0@alpha", "drupal/fontawesome": "^2.12", diff --git a/modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml b/modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml new file mode 100644 index 00000000..08570046 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml @@ -0,0 +1,7 @@ +name: Apigee Kickstart Monetization +description: "Handles monetization for Apigee Kickstart. Adds and configures a default product type for adding credit." +type: module +core: 8.x +package: Apigee Kickstart +dependencies: + - apigee_m10n:apigee_m10n diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml b/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml new file mode 100644 index 00000000..238f52ab --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml @@ -0,0 +1,2 @@ +notify_on: error_only +use_modal: false diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml new file mode 100644 index 00000000..02f80e35 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml @@ -0,0 +1,9 @@ +langcode: en +status: true +dependencies: { } +label: 'Add credit' +id: add_credit +purchasableEntityType: commerce_product_variation +orderType: default +traits: { } +locked: false diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml new file mode 100644 index 00000000..93a359d3 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml @@ -0,0 +1,17 @@ +langcode: en +status: true +dependencies: + module: + - apigee_m10n_add_credit +third_party_settings: + apigee_m10n_add_credit: + apigee_m10n_enable_add_credit: true + apigee_m10n_enable_skip_cart: true +id: add_credit +label: 'Add credit' +description: 'This product is used to add credit to prepaid balances.' +variationType: add_credit +multipleVariations: true +injectVariationFields: true +traits: { } +locked: false diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml new file mode 100644 index 00000000..54124fdc --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml @@ -0,0 +1,9 @@ +langcode: en +status: true +dependencies: { } +id: add_credit +label: 'Add credit' +orderItemType: add_credit +generateTitle: false +traits: { } +locked: false diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml new file mode 100644 index 00000000..a38fabeb --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml @@ -0,0 +1,36 @@ +langcode: en +status: true +dependencies: + config: + - commerce_order.commerce_order_item_type.add_credit + - core.entity_form_mode.commerce_order_item.add_to_cart + module: + - apigee_m10n_add_credit + - commerce_price + - commerce_product +id: commerce_order_item.add_credit.add_to_cart +targetEntityType: commerce_order_item +bundle: add_credit +mode: add_to_cart +content: + add_credit_target: + type: add_credit_target_entity + weight: 1 + region: content + settings: { } + third_party_settings: { } + purchased_entity: + type: commerce_product_variation_attributes + weight: 0 + region: content + settings: { } + third_party_settings: { } + unit_price: + type: commerce_price_default + weight: 2 + region: content + settings: { } + third_party_settings: { } +hidden: + created: true + quantity: true diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml new file mode 100644 index 00000000..c5d8f6f7 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml @@ -0,0 +1,79 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_type.add_credit + - field.field.commerce_product.add_credit.body + - field.field.commerce_product.add_credit.stores + module: + - commerce + - path + - text +id: commerce_product.add_credit.default +targetEntityType: commerce_product +bundle: add_credit +mode: default +content: + apigee_add_credit_enabled: + weight: 25 + region: content + settings: + display_label: true + third_party_settings: { } + type: boolean_checkbox + body: + type: text_textarea_with_summary + weight: 1 + settings: + rows: 9 + summary_rows: 3 + placeholder: '' + third_party_settings: { } + region: content + created: + type: datetime_timestamp + weight: 10 + region: content + settings: { } + third_party_settings: { } + path: + type: path + weight: 30 + region: content + settings: { } + third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 90 + region: content + third_party_settings: { } + stores: + type: commerce_entity_select + weight: -10 + settings: + hide_single_entity: true + autocomplete_threshold: 7 + autocomplete_size: 60 + autocomplete_placeholder: '' + third_party_settings: { } + region: content + title: + type: string_textfield + weight: -5 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + uid: + type: entity_reference_autocomplete + weight: 5 + region: content + settings: + match_operator: CONTAINS + size: 60 + placeholder: '' + third_party_settings: { } +hidden: { } diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml new file mode 100644 index 00000000..f6a52cb7 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml @@ -0,0 +1,39 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_variation_type.add_credit + module: + - apigee_m10n_add_credit +id: commerce_product_variation.add_credit.default +targetEntityType: commerce_product_variation +bundle: add_credit +mode: default +content: + apigee_price_range: + type: price_range_default + weight: 1 + region: content + settings: { } + third_party_settings: { } + sku: + type: string_textfield + weight: 0 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 2 + region: content + third_party_settings: { } +hidden: + created: true + list_price: true + price: true + title: true + uid: true diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml new file mode 100644 index 00000000..a8c8712e --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml @@ -0,0 +1,44 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_type.add_credit + - field.field.commerce_product.add_credit.body + - field.field.commerce_product.add_credit.stores + - field.field.commerce_product.add_credit.variations + module: + - commerce_product + - text +id: commerce_product.add_credit.default +targetEntityType: commerce_product +bundle: add_credit +mode: default +content: + body: + label: hidden + type: text_default + weight: 2 + settings: { } + third_party_settings: { } + region: content + title: + label: hidden + type: string + weight: 0 + region: content + settings: + link_to_entity: false + third_party_settings: { } + variations: + type: commerce_add_to_cart + weight: 1 + label: hidden + settings: + combine: true + third_party_settings: { } + region: content +hidden: + apigee_add_credit_enabled: true + created: true + stores: true + uid: true diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml new file mode 100644 index 00000000..481833c9 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml @@ -0,0 +1,17 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_variation_type.add_credit +id: commerce_product_variation.add_credit.default +targetEntityType: commerce_product_variation +bundle: add_credit +mode: default +content: { } +hidden: + apigee_price_range: true + list_price: true + price: true + product_id: true + sku: true + title: true diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml new file mode 100644 index 00000000..22871c9b --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml @@ -0,0 +1,21 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_type.add_credit + - field.storage.commerce_product.body + module: + - text +id: commerce_product.add_credit.body +field_name: body +entity_type: commerce_product +bundle: add_credit +label: Body +description: '' +required: false +translatable: true +default_value: { } +default_value_callback: null +settings: + display_summary: false +field_type: text_with_summary diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml new file mode 100644 index 00000000..ad7703a1 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml @@ -0,0 +1,20 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_type.add_credit + - field.storage.commerce_product.stores +id: commerce_product.add_credit.stores +field_name: stores +entity_type: commerce_product +bundle: add_credit +label: Stores +description: '' +required: true +translatable: false +default_value: { } +default_value_callback: null +settings: + handler: 'default:commerce_store' + handler_settings: { } +field_type: entity_reference diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml new file mode 100644 index 00000000..5adea712 --- /dev/null +++ b/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml @@ -0,0 +1,23 @@ +langcode: en +status: true +dependencies: + config: + - commerce_product.commerce_product_type.add_credit + - commerce_product.commerce_product_variation_type.add_credit + - field.storage.commerce_product.variations +id: commerce_product.add_credit.variations +field_name: variations +entity_type: commerce_product +bundle: add_credit +label: Variations +description: '' +required: true +translatable: false +default_value: { } +default_value_callback: null +settings: + handler: 'default:commerce_product_variation' + handler_settings: + target_bundles: + - add_credit +field_type: entity_reference diff --git a/src/ApigeeDevportalKickstartManager.php b/src/ApigeeDevportalKickstartManager.php new file mode 100644 index 00000000..b8a7ee24 --- /dev/null +++ b/src/ApigeeDevportalKickstartManager.php @@ -0,0 +1,68 @@ +getOrganization(); + + // Use cached result if available. + $monetization_status_cache_entry = $cache->get("apigee_m10n:org_monetization_status:{$org_id}"); + $monetization_status = $monetization_status_cache_entry ? $monetization_status_cache_entry->data : NULL; + + if (!$monetization_status) { + // Load organization and populate cache. + $org_controller = new OrganizationController($sdk_connector->getClient()); + + try { + /** @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface $org */ + $org = $org_controller->load($org_id); + $monetization_status = $org->getPropertyValue('features.isMonetizationEnabled') === 'true' ? 'enabled' : 'disabled'; + + $expire_time = new \DateTime('now + 5 minutes'); + $cache->set("apigee_m10n:org_monetization_status:{$org_id}", $monetization_status, $expire_time->getTimestamp()); + } catch (\Exception $e) { + return FALSE; + } + } + + return ($monetization_status === 'enabled'); + } + + return FALSE; + } +} diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php new file mode 100644 index 00000000..ed5ab485 --- /dev/null +++ b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php @@ -0,0 +1,527 @@ +configFactory = $config_factory; + $this->sdkConnector = $sdk_connector; + $this->moduleInstaller = $module_installer; + $this->entityTypeManager = $entity_type_manager; + $this->languageManager = $language_manager; + $this->keyRepository = $key_repository; + $this->currencyRepository = new CurrencyRepository(); + + try { + $organization_id = $this->sdkConnector->getOrganization(); + $client = $this->sdkConnector->getClient(); + + // TODO: Figure out if we need to cache these values here. + // This happens only once during installation. We probably do not need to + // cache these values? + $organization_controller = new OrganizationController($client); + $organization = $organization_controller->load($organization_id); + /** @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface $organization */ + if ($this->isMonetizable = $organization->getPropertyValue('features.isMonetizationEnabled') === 'true') { + // Set the organization. + $organization_profile_controller = new OrganizationProfileController($organization_id, $client); + $this->organization = $organization_profile_controller->load($organization_id); + + // Set supported currencies. + $supported_currency_controller = new SupportedCurrencyController($organization_id, $client); + $this->supportedCurrencies = $supported_currency_controller->getEntities(); + } + } + catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('apigee_edge.sdk_connector'), + $container->get('module_installer'), + $container->get('entity_type.manager'), + $container->get('language_manager'), + $container->get('key.repository') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'apigee_m10n_configuration_form'; + } + + public function buildForm(array $form, FormStateInterface $form_state) { + // Some messages stick around for installation tasks. Clear them all. + $this->messenger()->deleteAll(); + + $form['#title'] = $this->t('Configure Apigee Kickstart'); + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Continue'), + '#submit' => [[$this, 'skipStepSubmit']], + '#validate' => [], + '#limit_validation_errors' => [], + ]; + + // If monetization is not enabled, show a message and continue. + if (!$this->isMonetizable) { + $form['message'] = [ + '#theme' => 'status_messages', + '#message_list' => [ + MessengerInterface::TYPE_WARNING => [ + $this->t('Monetization is not enabled for your organization'), + ], + ], + ]; + + return $form; + } + + $form['modules'] = [ + '#type' => 'container', + '#tree' => TRUE, + ]; + + $form['modules']['apigee_m10n'] = [ + '#title' => $this->t('Enable monetization'), + '#type' => 'checkbox', + '#description' => $this->t('Enable monetization for your Apigee Edge organization.'), + ]; + + $form['modules']['apigee_m10n_add_credit'] = [ + '#title' => $this->t('Enable prepaid balance top up'), + '#type' => 'checkbox', + '#description' => $this->t('Allow users to add credit to their prepaid balances.'), + '#states' => [ + 'visible' => [ + 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], + ], + ], + ]; + + $form['store'] = [ + '#type' => 'details', + '#title' => $this->t('Store'), + '#open' => TRUE, + '#tree' => TRUE, + '#description' => $this->t('Create a store for handling prepaid balance top ups.'), + '#states' => [ + 'visible' => [ + 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + ], + ], + ]; + + $site_config = $this->configFactory()->get('system.site'); + $form['store']['name'] = [ + '#title' => $this->t('Name'), + '#type' => 'textfield', + '#placeholder' => $this->t('Name of store'), + '#default_value' => $site_config->get('name'), + '#states' => [ + 'required' => [ + 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + ], + ], + ]; + + $form['store']['mail'] = [ + '#title' => $this->t('Email'), + '#type' => 'email', + '#placeholder' => $this->t('admin@example.com'), + '#default_value' => $site_config->get('mail'), + '#description' => $this->t('Store email notifications are sent from this address.'), + '#states' => [ + 'required' => [ + 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + ], + ], + ]; + + $form['store']['default_currency'] = [ + '#type' => 'value', + '#value' => $this->organization->getCurrencyCode(), + ]; + + $form['store']['type'] = [ + '#type' => 'value', + '#value' => 'online', + ]; + + $form['store']['address'] = [ + '#title' => $this->t('Address'), + '#type' => 'address', + '#field_overrides' => [ + AddressField::GIVEN_NAME => FieldOverride::HIDDEN, + AddressField::FAMILY_NAME => FieldOverride::HIDDEN, + AddressField::ORGANIZATION => FieldOverride::HIDDEN, + AddressField::ADDRESS_LINE2 => FieldOverride::HIDDEN, + AddressField::POSTAL_CODE => FieldOverride::OPTIONAL, + ], + ]; + + if (count($this->supportedCurrencies)) { + $form['currencies'] = [ + '#type' => 'details', + '#title' => $this->t('Currencies'), + '#open' => TRUE, + '#description' => $this->t('Create a product to add credit for the following supported currencies.'), + '#states' => [ + 'visible' => [ + 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + ], + ], + ]; + + $currency_options = []; + $importable_currencies = $this->getImportableCurrencies(); + foreach ($this->supportedCurrencies as $currency) { + if ($currency->getStatus() === 'ACTIVE' && isset($importable_currencies[$currency->getName()])) { + $currency_options[$currency->getName()] = "{$currency->getDisplayName()} ({$currency->getName()})"; + } + } + + $form['currencies']['supported_currencies'] = [ + '#type' => 'checkboxes', + '#options' => $currency_options, + '#default_value' => array_keys($currency_options), + ]; + } + + // Add default address from organization. + if ($addresses = $this->organization->getAddresses()) { + /** @var \Apigee\Edge\Api\Monetization\Structure\Address $address */ + $address = reset($addresses); + + $form['store']['address']['#default_value'] = [ + 'address_line1' => $address->getAddress1(), + 'locality' => $address->getCity(), + 'administrative_area' => $address->getState(), + 'country_code' => $address->getCountry(), + 'postal_code' => $address->getZip(), + ]; + } + + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Save and continue'), + '#button_type' => 'primary', + ]; + + // Add a skip this step button. + $form['actions']['skip'] = [ + '#type' => 'submit', + '#value' => $this->t('Skip this step'), + '#submit' => [[$this, 'skipStepSubmit']], + '#validate' => [], + '#limit_validation_errors' => [], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { + $operations = [ + [[$this, 'installModules'], [$form_state]], + ]; + + if (in_array('apigee_m10n_add_credit', $modules)) { + $operations += [ + [[$this, 'importCurrencies'], [$form_state]], + [[$this, 'createStore'], [$form_state]], + [[$this, 'createProducts'], [$form_state]], + ]; + } + + $batch = [ + 'operations' => $operations, + 'title' => $this->t('Performing additional tasks'), + 'error_message' => $this->t('The installation has encountered an error.'), + 'progress_message' => $this->t('Completed @current out of @total tasks.'), + ]; + + return $batch; + } + } + + public function installModules(FormStateInterface $form_state, &$context) { + if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { + // Enable the apigee_kickstart_m10n module also. + // This holds all default config for m10n. + $modules[] = 'apigee_kickstart_m10n'; + + // Enable the modules. + try { + $this->moduleInstaller->install($modules); + } catch (MissingDependencyException $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + } + + if (!isset($context['sandbox']['progress'])) { + $context['sandbox']['progress'] = 0; + } + + $context['sandbox']['progress']++; + $context['message'] = $this->t('Installed monetization modules'); + } + + public function importCurrencies(FormStateInterface $form_state, &$context) { + if ($currencies = array_filter($form_state->getValue('supported_currencies'))) { + /** @var CurrencyImporterInterface $currency_importer */ + // This cannot be injected because this has to be run after the required + // modules is installed. + $currency_importer = \Drupal::service('commerce_price.currency_importer'); + + foreach ($currencies as $currency_code) { + // Import the currency. + $currency_importer->import($currency_code); + + // Save it to context. + $context['results']['currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; + } + } + + $context['sandbox']['progress']++; + $context['message'] = $this->t('Imported supported currencies'); + } + + public function createStore(FormStateInterface $form_state, &$context) { + // Create a store. + $store = $this->entityTypeManager->getStorage('commerce_store') + ->create($form_state->getValue('store')); + $store->save(); + + // Create a payment gateway. + $this->entityTypeManager->getStorage('commerce_payment_gateway')->create([ + 'id' => 'default', + 'label' => 'Default', + 'plugin' => 'manual', + ])->save(); + + // Save to context. + $context['results']['store'] = $store; + + $context['sandbox']['progress']++; + $context['message'] = $this->t('Created a default store and payment gateway'); + } + + public function createProducts(FormStateInterface $form_state, &$context) { + // If we have currencies and a store, create products. + if (count($context['results']['currencies']) && isset($context['results']['store'])) { + $add_credit_products = []; + + /** @var \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface $currency */ + foreach ($context['results']['currencies'] as $currency) { + // Create a product variation for this currency. + $variation = $this->entityTypeManager->getStorage('commerce_product_variation') + ->create([ + 'type' => 'add_credit', + 'sku' => "ADDCREDIT-{$currency->getName()}", + 'title' => $currency->getName(), + 'status' => 1, + 'price' => new Price((string) $currency->getMinimumTopUpAmount(), $currency->getId()), + ]); + $variation->save(); + + // Create an add credit product for this currency. + $product = $this->entityTypeManager->getStorage('commerce_product') + ->create([ + 'title' => $currency->getName(), + 'type' => 'add_credit', + 'stores' => [$context['results']['store']], + 'variations' => [$variation], + AddCreditConfig::ADD_CREDIT_ENABLED_FIELD_NAME => 1, + ]); + $product->save(); + + $add_credit_products[$currency->getId()] = [ + 'product_id' => $product->id(), + ]; + } + + // Save config. + $this->configFactory() + ->getEditable(AddCreditConfig::CONFIG_NAME) + ->set('products', $add_credit_products) + ->save(); + } + + $context['sandbox']['progress']++; + $context['message'] = $this->t('Created the default products'); + } + + /** + * Provides a submit handler for the skip step button. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ + public function skipStepSubmit(array $form, FormStateInterface $form_state) { + global $install_state; + $install_state['completed_task'] = install_verify_completed_task(); + } + + protected function getImportableCurrencies() { + $language = $this->languageManager->getConfigOverrideLanguage() ?: $this->languageManager->getCurrentLanguage(); + return $this->currencyRepository->getAll($language->getId()); + } + + protected function isMonetizable(OrganizationInterface $organization) { + // We can only check for monetization if we have a valid organization and + // a key configured. + if ($organization && ($active_key_id = $this->configFactory->get(AuthenticationForm::CONFIG_NAME)->get('active_key')) && ($this->keyRepository->getKey($active_key_id))) { + return $organization->getPropertyValue('features.isMonetizationEnabled') === 'true'; + } + return FALSE; + } + +} diff --git a/src/Installer/Form/ApigeeEdgeConfigurationForm.php b/src/Installer/Form/ApigeeEdgeConfigurationForm.php index 034c9cce..f41dfca6 100644 --- a/src/Installer/Form/ApigeeEdgeConfigurationForm.php +++ b/src/Installer/Form/ApigeeEdgeConfigurationForm.php @@ -109,6 +109,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->oauthTokenStorage->removeTokenFile(); } +// if ($buildInfo = $form_state->getBuildInfo()) { +// $buildInfo['args'][0]['monetization_enabled'] = \Drupal::service('apigee_devportal_kickstart.manager')->isMonetizationEnabled(); +// $form_state->setBuildInfo($buildInfo); +// } + + global $install_state; + $install_state['completed_task'] = install_verify_completed_task(); + parent::submitForm($form, $form_state); } From e3213b1a37ae116e9c90c3a855e59b096b9bdfd1 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 12:16:30 +0400 Subject: [PATCH 02/28] [DRUP-728] refactor batch operations to a service --- apigee_devportal_kickstart.install | 67 ++++-- apigee_devportal_kickstart.services.yml | 5 +- .../apigee_m10n_add_credit.config.yml | 2 - ...apigee_kickstart_m10n_add_credit.info.yml} | 1 + ...er.commerce_order_item_type.add_credit.yml | 0 ...oduct.commerce_product_type.add_credit.yml | 0 ...erce_product_variation_type.add_credit.yml | 0 ...erce_order_item.add_credit.add_to_cart.yml | 0 ...ay.commerce_product.add_credit.default.yml | 0 ...e_product_variation.add_credit.default.yml | 0 ...ay.commerce_product.add_credit.default.yml | 0 ...e_product_variation.add_credit.default.yml | 0 ...field.commerce_product.add_credit.body.yml | 0 ...eld.commerce_product.add_credit.stores.yml | 0 ...commerce_product.add_credit.variations.yml | 0 src/ApigeeDevportalKickstartManager.php | 68 ------- .../ApigeeDevportalKickstartTasksManager.php | 191 ++++++++++++++++++ ...evportalKickstartTasksManagerInterface.php | 78 +++++++ ...geeDevportalKickstartConfigurationForm.php | 162 ++++++++------- 19 files changed, 398 insertions(+), 176 deletions(-) delete mode 100644 modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml rename modules/custom/{apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml => apigee_kickstart_m10n_add_credit/apigee_kickstart_m10n_add_credit.info.yml} (83%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/commerce_order.commerce_order_item_type.add_credit.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/commerce_product.commerce_product_type.add_credit.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/commerce_product.commerce_product_variation_type.add_credit.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/core.entity_form_display.commerce_product.add_credit.default.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/core.entity_form_display.commerce_product_variation.add_credit.default.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/core.entity_view_display.commerce_product.add_credit.default.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/core.entity_view_display.commerce_product_variation.add_credit.default.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/field.field.commerce_product.add_credit.body.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/field.field.commerce_product.add_credit.stores.yml (100%) rename modules/custom/{apigee_kickstart_m10n/config/optional => apigee_kickstart_m10n_add_credit/config/install}/field.field.commerce_product.add_credit.variations.yml (100%) delete mode 100644 src/ApigeeDevportalKickstartManager.php create mode 100644 src/Installer/ApigeeDevportalKickstartTasksManager.php create mode 100644 src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index b5b6d093..db6b2b1d 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -23,6 +23,7 @@ * Install, update and uninstall functions for Apigee Kickstart profile. */ +use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager; use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartConfigurationForm; use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; @@ -107,6 +108,11 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { 'display_name' => t('Configure kickstart'), 'type' => 'form', ], + 'apigee_devportal_setup_monetization' => [ + 'display_name' => t('Setup monetization'), + 'type' => 'batch', +// 'display' => isset($install_state['m10n_config']), + ], DemoInstallForm::class => [ 'display_name' => t('Install demo content'), 'type' => 'form', @@ -118,6 +124,46 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { ]; } +/** + * Install task for setting up monetization and additional modules. + * + * @param array $install_state + * The install state. + * + * @return array + * A batch definition. + */ +function apigee_devportal_setup_monetization(array &$install_state) { + if (isset($install_state['m10n_config']) + && ($config = $install_state['m10n_config']) + && ($modules = array_keys(array_filter($config['modules'])))) { + + // Add an operations to install modules. + $operations = [ + [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$modules]], + ]; + + // Perform additional tasks for apigee_m10n_add_credit. + if (in_array('apigee_kickstart_m10n_add_credit', $modules)) { + $operations += [ + [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], + [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], + [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], + [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], + ]; + } + + $batch = [ + 'operations' => $operations, + 'title' => t('Performing additional tasks'), + 'error_message' => t('The installation has encountered an error.'), + 'progress_message' => t('Completed @current out of @total tasks.'), + ]; + + return $batch; + } +} + /** * Install the theme. * @@ -144,24 +190,3 @@ function apigee_devportal_kickstart_theme_setup(array &$install_state) { ->set('use_admin_theme', TRUE) ->save(TRUE); } - -//function apigee_devportal_kickstart_isMonetizationEnabled() { -// if (Drupal::moduleHandler()->moduleExists('apigee_edge')) { -// try { -// $active_key_id = Drupal::configFactory() -// ->get(AuthenticationForm::CONFIG_NAME) -// ->get('active_key'); -// if ($active_key = Drupal::service('key.repository') -// ->getKey($active_key_id)) { -// $sdk_connector = \Drupal::service('apigee_edge.sdk_connector'); -// $org_controller = new OrganizationController($sdk_connector->getClient()); -// $org = $org_controller->load($sdk_connector->getOrganization()); -// return $org->getPropertyValue('features.isMonetizationEnabled') === 'true'; -// } -// } catch (\Exception $e) { -// return FALSE; -// } -// } -// -// return FALSE; -//} diff --git a/apigee_devportal_kickstart.services.yml b/apigee_devportal_kickstart.services.yml index 1daa811f..d71071b7 100644 --- a/apigee_devportal_kickstart.services.yml +++ b/apigee_devportal_kickstart.services.yml @@ -1,3 +1,4 @@ services: - apigee_devportal_kickstart.manager: - class: Drupal\apigee_devportal_kickstart\ApigeeDevportalKickstartManager + apigee_devportal_kickstart.tasks_manager: + class: Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager + arguments: ['@entity_type.manager', '@module_installer', '@config.factory'] diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml b/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml deleted file mode 100644 index 238f52ab..00000000 --- a/modules/custom/apigee_kickstart_m10n/config/optional/apigee_m10n_add_credit.config.yml +++ /dev/null @@ -1,2 +0,0 @@ -notify_on: error_only -use_modal: false diff --git a/modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml b/modules/custom/apigee_kickstart_m10n_add_credit/apigee_kickstart_m10n_add_credit.info.yml similarity index 83% rename from modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/apigee_kickstart_m10n_add_credit.info.yml index 08570046..5804f11f 100644 --- a/modules/custom/apigee_kickstart_m10n/apigee_kickstart_m10n.info.yml +++ b/modules/custom/apigee_kickstart_m10n_add_credit/apigee_kickstart_m10n_add_credit.info.yml @@ -5,3 +5,4 @@ core: 8.x package: Apigee Kickstart dependencies: - apigee_m10n:apigee_m10n + - apigee_m10n_add_credit:apigee_m10n_add_credit diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_order.commerce_order_item_type.add_credit.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/commerce_order.commerce_order_item_type.add_credit.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_order.commerce_order_item_type.add_credit.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_product.commerce_product_type.add_credit.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_type.add_credit.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_product.commerce_product_type.add_credit.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_product.commerce_product_variation_type.add_credit.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/commerce_product.commerce_product_variation_type.add_credit.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/commerce_product.commerce_product_variation_type.add_credit.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_product.add_credit.default.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product.add_credit.default.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_product.add_credit.default.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_product_variation.add_credit.default.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/core.entity_form_display.commerce_product_variation.add_credit.default.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_product_variation.add_credit.default.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_view_display.commerce_product.add_credit.default.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product.add_credit.default.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_view_display.commerce_product.add_credit.default.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_view_display.commerce_product_variation.add_credit.default.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/core.entity_view_display.commerce_product_variation.add_credit.default.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_view_display.commerce_product_variation.add_credit.default.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.body.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.body.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.body.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.stores.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.stores.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.stores.yml diff --git a/modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.variations.yml similarity index 100% rename from modules/custom/apigee_kickstart_m10n/config/optional/field.field.commerce_product.add_credit.variations.yml rename to modules/custom/apigee_kickstart_m10n_add_credit/config/install/field.field.commerce_product.add_credit.variations.yml diff --git a/src/ApigeeDevportalKickstartManager.php b/src/ApigeeDevportalKickstartManager.php deleted file mode 100644 index b8a7ee24..00000000 --- a/src/ApigeeDevportalKickstartManager.php +++ /dev/null @@ -1,68 +0,0 @@ -getOrganization(); - - // Use cached result if available. - $monetization_status_cache_entry = $cache->get("apigee_m10n:org_monetization_status:{$org_id}"); - $monetization_status = $monetization_status_cache_entry ? $monetization_status_cache_entry->data : NULL; - - if (!$monetization_status) { - // Load organization and populate cache. - $org_controller = new OrganizationController($sdk_connector->getClient()); - - try { - /** @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface $org */ - $org = $org_controller->load($org_id); - $monetization_status = $org->getPropertyValue('features.isMonetizationEnabled') === 'true' ? 'enabled' : 'disabled'; - - $expire_time = new \DateTime('now + 5 minutes'); - $cache->set("apigee_m10n:org_monetization_status:{$org_id}", $monetization_status, $expire_time->getTimestamp()); - } catch (\Exception $e) { - return FALSE; - } - } - - return ($monetization_status === 'enabled'); - } - - return FALSE; - } -} diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php new file mode 100644 index 00000000..f0707832 --- /dev/null +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -0,0 +1,191 @@ +entityTypeManager = $entity_type_manager; +// $this->moduleInstaller = $module_installer; +// $this->configFactory = $config_factory; +// } + + /** + * {@inheritdoc} + */ + public static function installModules(array $modules, &$context) { + try { + \Drupal::service('module_installer')->install($modules); + + if (!isset($context['sandbox']['progress'])) { + $context['sandbox']['progress'] = 0; + } + + $context['sandbox']['progress']++; + $context['message'] = t('Installed monetization modules.'); + } catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + } + + /** + * {@inheritdoc} + */ + public static function importCurrencies(array $currencies, &$context) { + foreach ($currencies as $currency) { + // Import the currency. + \Drupal::service('commerce_price.currency_importer')->import($currency->getName()); + } + + $context['sandbox']['progress']++; + $context['message'] = t('Imported supported currencies.'); + } + + /** + * {@inheritdoc} + */ + public static function createStore(array $values, &$context) { + try { + $store = \Drupal::entityTypeManager()->getStorage('commerce_store') + ->create($values); + $store->save(); + + // Save to context. + $context['results']['store'] = $store; + $context['sandbox']['progress']++; + $context['message'] = t('Created a default store.'); + } catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + } + + /** + * {@inheritdoc} + */ + public static function createPaymentGateway(array $values, &$context) { + try { + $gateway = \Drupal::entityTypeManager()->getStorage('commerce_payment_gateway') + ->create($values); + $gateway->save(); + + // Save to context. + $context['results']['$gateway'] = $gateway; + $context['sandbox']['progress']++; + $context['message'] = t('Created a default payment gateway.'); + } catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + } + + /** + * {@inheritdoc} + */ + public static function createProducts(array $currencies, &$context) { + if (isset($context['results']['store'])) { + $add_credit_products = []; + + /** @var \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface $currency */ + foreach ($currencies as $currency) { + try { + // Create a product variation for this currency. + $variation = \Drupal::entityTypeManager()->getStorage('commerce_product_variation') + ->create([ + 'type' => 'add_credit', + 'sku' => "ADD-CREDIT-{$currency->getName()}", + 'title' => $currency->getName(), + 'status' => 1, + 'price' => new Price((string) $currency->getMinimumTopUpAmount(), $currency->getId()), + ]); + $variation->save(); + + // Create an add credit product for this currency. + $product = \Drupal::entityTypeManager()->getStorage('commerce_product') + ->create([ + 'title' => $currency->getName(), + 'type' => 'add_credit', + 'stores' => [$context['results']['store']], + 'variations' => [$variation], + AddCreditConfig::ADD_CREDIT_ENABLED_FIELD_NAME => 1, + ]); + $product->save(); + + $add_credit_products[$currency->getId()] = [ + 'product_id' => $product->id(), + ]; + + // Save config. + \Drupal::configFactory() + ->getEditable(AddCreditConfig::CONFIG_NAME) + ->set('products', $add_credit_products) + ->save(); + + $context['sandbox']['progress']++; + $context['message'] = t('Created the default products.'); + } catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + } + } + } + +} diff --git a/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php b/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php new file mode 100644 index 00000000..99ac7a9e --- /dev/null +++ b/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php @@ -0,0 +1,78 @@ + $this->t('Enable monetization for your Apigee Edge organization.'), ]; - $form['modules']['apigee_m10n_add_credit'] = [ + $form['modules']['apigee_kickstart_m10n_add_credit'] = [ '#title' => $this->t('Enable prepaid balance top up'), '#type' => 'checkbox', '#description' => $this->t('Allow users to add credit to their prepaid balances.'), @@ -248,7 +248,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#states' => [ 'visible' => [ 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], - 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_kickstart_m10n_add_credit]"]' => ['checked' => TRUE], ], ], ]; @@ -261,7 +261,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $site_config->get('name'), '#states' => [ 'required' => [ - 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_kickstart_m10n_add_credit]"]' => ['checked' => TRUE], ], ], ]; @@ -274,7 +274,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('Store email notifications are sent from this address.'), '#states' => [ 'required' => [ - 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_kickstart_m10n_add_credit]"]' => ['checked' => TRUE], ], ], ]; @@ -310,7 +310,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#states' => [ 'visible' => [ 'input[name="modules[apigee_m10n]"]' => ['checked' => TRUE], - 'input[name="modules[apigee_m10n_add_credit]"]' => ['checked' => TRUE], + 'input[name="modules[apigee_kickstart_m10n_add_credit]"]' => ['checked' => TRUE], ], ], ]; @@ -366,92 +366,88 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { - $operations = [ - [[$this, 'installModules'], [$form_state]], - ]; - - if (in_array('apigee_m10n_add_credit', $modules)) { - $operations += [ - [[$this, 'importCurrencies'], [$form_state]], - [[$this, 'createStore'], [$form_state]], - [[$this, 'createProducts'], [$form_state]], - ]; - } - - $batch = [ - 'operations' => $operations, - 'title' => $this->t('Performing additional tasks'), - 'error_message' => $this->t('The installation has encountered an error.'), - 'progress_message' => $this->t('Completed @current out of @total tasks.'), - ]; - - return $batch; - } - } - - public function installModules(FormStateInterface $form_state, &$context) { - if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { - // Enable the apigee_kickstart_m10n module also. - // This holds all default config for m10n. - $modules[] = 'apigee_kickstart_m10n'; - - // Enable the modules. - try { - $this->moduleInstaller->install($modules); - } catch (MissingDependencyException $exception) { - watchdog_exception('apigee_kickstart', $exception); - } - } - - if (!isset($context['sandbox']['progress'])) { - $context['sandbox']['progress'] = 0; - } - - $context['sandbox']['progress']++; - $context['message'] = $this->t('Installed monetization modules'); - } - - public function importCurrencies(FormStateInterface $form_state, &$context) { - if ($currencies = array_filter($form_state->getValue('supported_currencies'))) { - /** @var CurrencyImporterInterface $currency_importer */ - // This cannot be injected because this has to be run after the required - // modules is installed. - $currency_importer = \Drupal::service('commerce_price.currency_importer'); - - foreach ($currencies as $currency_code) { - // Import the currency. - $currency_importer->import($currency_code); + global $install_state; + $form_state->cleanValues(); + $install_state['m10n_config'] = $form_state->getValues(); - // Save it to context. - $context['results']['currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; + // Update the supported currencies. + if (isset($install_state['m10n_config']['supported_currencies'])) { + foreach ($install_state['m10n_config']['supported_currencies'] as $currency_code) { + $install_state['m10n_config']['supported_currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; } } - $context['sandbox']['progress']++; - $context['message'] = $this->t('Imported supported currencies'); - } - - public function createStore(FormStateInterface $form_state, &$context) { - // Create a store. - $store = $this->entityTypeManager->getStorage('commerce_store') - ->create($form_state->getValue('store')); - $store->save(); - - // Create a payment gateway. - $this->entityTypeManager->getStorage('commerce_payment_gateway')->create([ + // Add values for a payment gateway. + // TODO: Figure out if this should be configurable in the form. + $install_state['m10n_config']['gateway'] = [ 'id' => 'default', 'label' => 'Default', 'plugin' => 'manual', - ])->save(); - - // Save to context. - $context['results']['store'] = $store; - - $context['sandbox']['progress']++; - $context['message'] = $this->t('Created a default store and payment gateway'); + ]; } +// public function installModules(FormStateInterface $form_state, &$context) { +// if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { +// // Enable the apigee_kickstart_m10n module also. +// // This holds all default config for m10n. +// $modules[] = 'apigee_kickstart_m10n'; +// +// // Enable the modules. +// try { +// $this->moduleInstaller->install($modules); +// } catch (MissingDependencyException $exception) { +// watchdog_exception('apigee_kickstart', $exception); +// } +// } +// +// if (!isset($context['sandbox']['progress'])) { +// $context['sandbox']['progress'] = 0; +// } +// +// $context['sandbox']['progress']++; +// $context['message'] = $this->t('Installed monetization modules'); +// } + +// public function importCurrencies(FormStateInterface $form_state, &$context) { +// if ($currencies = array_filter($form_state->getValue('supported_currencies'))) { +// /** @var CurrencyImporterInterface $currency_importer */ +// // This cannot be injected because this has to be run after the required +// // modules is installed. +// $currency_importer = \Drupal::service('commerce_price.currency_importer'); +// +// foreach ($currencies as $currency_code) { +// // Import the currency. +// $currency_importer->import($currency_code); +// +// // Save it to context. +// $context['results']['currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; +// } +// } +// +// $context['sandbox']['progress']++; +// $context['message'] = $this->t('Imported supported currencies'); +// } + +// public function createStore(FormStateInterface $form_state, &$context) { +// // Create a store. +// $store = $this->entityTypeManager->getStorage('commerce_store') +// ->create($form_state->getValue('store')); +// $store->save(); +// +// // Create a payment gateway. +// $this->entityTypeManager->getStorage('commerce_payment_gateway')->create([ +// 'id' => 'default', +// 'label' => 'Default', +// 'plugin' => 'manual', +// ])->save(); +// +// // Save to context. +// $context['results']['store'] = $store; +// +// $context['sandbox']['progress']++; +// $context['message'] = $this->t('Created a default store and payment gateway'); +// } + public function createProducts(FormStateInterface $form_state, &$context) { // If we have currencies and a store, create products. if (count($context['results']['currencies']) && isset($context['results']['store'])) { From 9f02e6c4f10dd991c6058d1dfc026a4b9163bd4d Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 13:00:43 +0400 Subject: [PATCH 03/28] [DRUP-728] refactor and clean up configuration form --- apigee_devportal_kickstart.install | 14 +- ...geeDevportalKickstartConfigurationForm.php | 244 ++++-------------- 2 files changed, 51 insertions(+), 207 deletions(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index db6b2b1d..cb7ee487 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -111,7 +111,6 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { 'apigee_devportal_setup_monetization' => [ 'display_name' => t('Setup monetization'), 'type' => 'batch', -// 'display' => isset($install_state['m10n_config']), ], DemoInstallForm::class => [ 'display_name' => t('Install demo content'), @@ -134,17 +133,14 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { * A batch definition. */ function apigee_devportal_setup_monetization(array &$install_state) { - if (isset($install_state['m10n_config']) - && ($config = $install_state['m10n_config']) - && ($modules = array_keys(array_filter($config['modules'])))) { - + if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) { // Add an operations to install modules. $operations = [ - [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$modules]], + [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$config['modules']]], ]; - // Perform additional tasks for apigee_m10n_add_credit. - if (in_array('apigee_kickstart_m10n_add_credit', $modules)) { + // Perform additional tasks for apigee_kickstart_m10n_add_credit. + if (in_array('apigee_kickstart_m10n_add_credit', $config['modules'])) { $operations += [ [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], @@ -155,7 +151,7 @@ function apigee_devportal_setup_monetization(array &$install_state) { $batch = [ 'operations' => $operations, - 'title' => t('Performing additional tasks'), + 'title' => t('Setting up monetization'), 'error_message' => t('The installation has encountered an error.'), 'progress_message' => t('Completed @current out of @total tasks.'), ]; diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php index 27b06716..1b78465b 100644 --- a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php +++ b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php @@ -21,26 +21,16 @@ namespace Drupal\apigee_devportal_kickstart\Installer\Form; use Apigee\Edge\Api\Management\Controller\OrganizationController; -use Apigee\Edge\Api\Management\Entity\OrganizationInterface; use Apigee\Edge\Api\Monetization\Controller\OrganizationProfileController; use Apigee\Edge\Api\Monetization\Controller\SupportedCurrencyController; use CommerceGuys\Addressing\AddressFormat\AddressField; use CommerceGuys\Addressing\AddressFormat\FieldOverride; use CommerceGuys\Intl\Currency\CurrencyRepository; -use Drupal\apigee_edge\Form\AuthenticationForm; use Drupal\apigee_edge\SDKConnectorInterface; -use Drupal\apigee_kickstart_m10n_add_credit\AddCreditConfig; -use Drupal\commerce_price\CurrencyImporterInterface; -use Drupal\commerce_price\Price; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\MissingDependencyException; -use Drupal\Core\Extension\ModuleInstallerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Messenger\MessengerInterface; -use Drupal\key\KeyRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -48,13 +38,6 @@ */ class ApigeeDevportalKickstartConfigurationForm extends FormBase { - /** - * The factory for configuration objects. - * - * @var \Drupal\Core\Config\ConfigFactoryInterface - */ - protected $configFactory; - /** * SDK connector service. * @@ -62,20 +45,6 @@ class ApigeeDevportalKickstartConfigurationForm extends FormBase { */ protected $sdkConnector; - /** - * The Drupal module installer. - * - * @var \Drupal\Core\Extension\ModuleInstallerInterface - */ - protected $moduleInstaller; - - /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - /** * The library's currency repository. * @@ -111,34 +80,17 @@ class ApigeeDevportalKickstartConfigurationForm extends FormBase { */ protected $isMonetizable; - /** - * @var \Drupal\key\KeyRepositoryInterface - */ - protected $keyRepository; - /** * ApigeeM10nConfigurationForm constructor. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The factory for configuration objects. * @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector * SDK connector service. - * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer - * The Drupal module installer. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. - * @param \Drupal\key\KeyRepositoryInterface $key_repository - * The key repository. */ - public function __construct(ConfigFactoryInterface $config_factory, SDKConnectorInterface $sdk_connector, ModuleInstallerInterface $module_installer, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, KeyRepositoryInterface $key_repository) { - $this->configFactory = $config_factory; + public function __construct(SDKConnectorInterface $sdk_connector, LanguageManagerInterface $language_manager) { $this->sdkConnector = $sdk_connector; - $this->moduleInstaller = $module_installer; - $this->entityTypeManager = $entity_type_manager; $this->languageManager = $language_manager; - $this->keyRepository = $key_repository; $this->currencyRepository = new CurrencyRepository(); try { @@ -172,12 +124,8 @@ public function __construct(ConfigFactoryInterface $config_factory, SDKConnector */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory'), $container->get('apigee_edge.sdk_connector'), - $container->get('module_installer'), - $container->get('entity_type.manager'), - $container->get('language_manager'), - $container->get('key.repository') + $container->get('language_manager') ); } @@ -185,9 +133,12 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function getFormId() { - return 'apigee_m10n_configuration_form'; + return 'apigee_devportal_kickstart_configuration_form'; } + /** + * {@inheritdoc} + */ public function buildForm(array $form, FormStateInterface $form_state) { // Some messages stick around for installation tasks. Clear them all. $this->messenger()->deleteAll(); @@ -301,6 +252,20 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; + // Add default address from organization. + if ($addresses = $this->organization->getAddresses()) { + /** @var \Apigee\Edge\Api\Monetization\Structure\Address $address */ + $address = reset($addresses); + + $form['store']['address']['#default_value'] = [ + 'address_line1' => $address->getAddress1(), + 'locality' => $address->getCity(), + 'administrative_area' => $address->getState(), + 'country_code' => $address->getCountry(), + 'postal_code' => $address->getZip(), + ]; + } + if (count($this->supportedCurrencies)) { $form['currencies'] = [ '#type' => 'details', @@ -330,20 +295,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; } - // Add default address from organization. - if ($addresses = $this->organization->getAddresses()) { - /** @var \Apigee\Edge\Api\Monetization\Structure\Address $address */ - $address = reset($addresses); - - $form['store']['address']['#default_value'] = [ - 'address_line1' => $address->getAddress1(), - 'locality' => $address->getCity(), - 'administrative_area' => $address->getState(), - 'country_code' => $address->getCountry(), - 'postal_code' => $address->getZip(), - ]; - } - $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Save and continue'), @@ -366,131 +317,31 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - global $install_state; $form_state->cleanValues(); - $install_state['m10n_config'] = $form_state->getValues(); - - // Update the supported currencies. - if (isset($install_state['m10n_config']['supported_currencies'])) { - foreach ($install_state['m10n_config']['supported_currencies'] as $currency_code) { - $install_state['m10n_config']['supported_currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; + $values = $form_state->getValues(); + $values['modules'] = array_keys(array_filter($values['modules'])); + + if (count($values['modules'])) { + // Update the supported currencies. + if (isset($values['supported_currencies'])) { + foreach ($values['supported_currencies'] as $currency_code) { + $values['supported_currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; + } } - } - - // Add values for a payment gateway. - // TODO: Figure out if this should be configurable in the form. - $install_state['m10n_config']['gateway'] = [ - 'id' => 'default', - 'label' => 'Default', - 'plugin' => 'manual', - ]; - } -// public function installModules(FormStateInterface $form_state, &$context) { -// if ($modules = array_keys(array_filter($form_state->getValue('modules')))) { -// // Enable the apigee_kickstart_m10n module also. -// // This holds all default config for m10n. -// $modules[] = 'apigee_kickstart_m10n'; -// -// // Enable the modules. -// try { -// $this->moduleInstaller->install($modules); -// } catch (MissingDependencyException $exception) { -// watchdog_exception('apigee_kickstart', $exception); -// } -// } -// -// if (!isset($context['sandbox']['progress'])) { -// $context['sandbox']['progress'] = 0; -// } -// -// $context['sandbox']['progress']++; -// $context['message'] = $this->t('Installed monetization modules'); -// } - -// public function importCurrencies(FormStateInterface $form_state, &$context) { -// if ($currencies = array_filter($form_state->getValue('supported_currencies'))) { -// /** @var CurrencyImporterInterface $currency_importer */ -// // This cannot be injected because this has to be run after the required -// // modules is installed. -// $currency_importer = \Drupal::service('commerce_price.currency_importer'); -// -// foreach ($currencies as $currency_code) { -// // Import the currency. -// $currency_importer->import($currency_code); -// -// // Save it to context. -// $context['results']['currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; -// } -// } -// -// $context['sandbox']['progress']++; -// $context['message'] = $this->t('Imported supported currencies'); -// } - -// public function createStore(FormStateInterface $form_state, &$context) { -// // Create a store. -// $store = $this->entityTypeManager->getStorage('commerce_store') -// ->create($form_state->getValue('store')); -// $store->save(); -// -// // Create a payment gateway. -// $this->entityTypeManager->getStorage('commerce_payment_gateway')->create([ -// 'id' => 'default', -// 'label' => 'Default', -// 'plugin' => 'manual', -// ])->save(); -// -// // Save to context. -// $context['results']['store'] = $store; -// -// $context['sandbox']['progress']++; -// $context['message'] = $this->t('Created a default store and payment gateway'); -// } - - public function createProducts(FormStateInterface $form_state, &$context) { - // If we have currencies and a store, create products. - if (count($context['results']['currencies']) && isset($context['results']['store'])) { - $add_credit_products = []; - - /** @var \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface $currency */ - foreach ($context['results']['currencies'] as $currency) { - // Create a product variation for this currency. - $variation = $this->entityTypeManager->getStorage('commerce_product_variation') - ->create([ - 'type' => 'add_credit', - 'sku' => "ADDCREDIT-{$currency->getName()}", - 'title' => $currency->getName(), - 'status' => 1, - 'price' => new Price((string) $currency->getMinimumTopUpAmount(), $currency->getId()), - ]); - $variation->save(); - - // Create an add credit product for this currency. - $product = $this->entityTypeManager->getStorage('commerce_product') - ->create([ - 'title' => $currency->getName(), - 'type' => 'add_credit', - 'stores' => [$context['results']['store']], - 'variations' => [$variation], - AddCreditConfig::ADD_CREDIT_ENABLED_FIELD_NAME => 1, - ]); - $product->save(); - - $add_credit_products[$currency->getId()] = [ - 'product_id' => $product->id(), - ]; - } + // Add values for a payment gateway. + // TODO: Figure out if this should be configurable in the form. + $values['gateway'] = [ + 'id' => 'default', + 'label' => 'Default', + 'plugin' => 'manual', + ]; - // Save config. - $this->configFactory() - ->getEditable(AddCreditConfig::CONFIG_NAME) - ->set('products', $add_credit_products) - ->save(); + // Save to install state. + $buildInfo = $form_state->getBuildInfo(); + $buildInfo['args'][0]['m10n_config'] = $values; + $form_state->setBuildInfo($buildInfo); } - - $context['sandbox']['progress']++; - $context['message'] = $this->t('Created the default products'); } /** @@ -506,18 +357,15 @@ public function skipStepSubmit(array $form, FormStateInterface $form_state) { $install_state['completed_task'] = install_verify_completed_task(); } + /** + * Helper to get importable currencies. + * + * @return array|\CommerceGuys\Intl\Currency\Currency[] + * An array of importable currencies. + */ protected function getImportableCurrencies() { $language = $this->languageManager->getConfigOverrideLanguage() ?: $this->languageManager->getCurrentLanguage(); return $this->currencyRepository->getAll($language->getId()); } - protected function isMonetizable(OrganizationInterface $organization) { - // We can only check for monetization if we have a valid organization and - // a key configured. - if ($organization && ($active_key_id = $this->configFactory->get(AuthenticationForm::CONFIG_NAME)->get('active_key')) && ($this->keyRepository->getKey($active_key_id))) { - return $organization->getPropertyValue('features.isMonetizationEnabled') === 'true'; - } - return FALSE; - } - } From 828ec8818940e720fea3bcb0adefd55a405a4bdf Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 14:55:32 +0400 Subject: [PATCH 04/28] [DRUP-728] remove the task manager service --- apigee_devportal_kickstart.services.yml | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 apigee_devportal_kickstart.services.yml diff --git a/apigee_devportal_kickstart.services.yml b/apigee_devportal_kickstart.services.yml deleted file mode 100644 index d71071b7..00000000 --- a/apigee_devportal_kickstart.services.yml +++ /dev/null @@ -1,4 +0,0 @@ -services: - apigee_devportal_kickstart.tasks_manager: - class: Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager - arguments: ['@entity_type.manager', '@module_installer', '@config.factory'] From 85a8802949cd07a4bf5d71a9509f3bd7045651d6 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 14:57:25 +0400 Subject: [PATCH 05/28] [DRUP-728] rename the submit button for edge config form --- src/Installer/Form/ApigeeEdgeConfigurationForm.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Installer/Form/ApigeeEdgeConfigurationForm.php b/src/Installer/Form/ApigeeEdgeConfigurationForm.php index f41dfca6..cdd87bef 100644 --- a/src/Installer/Form/ApigeeEdgeConfigurationForm.php +++ b/src/Installer/Form/ApigeeEdgeConfigurationForm.php @@ -55,6 +55,9 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Hide the test_connection fields. $form['test_connection']['#access'] = FALSE; + // Rename the submit button. + $form['actions']['submit']['#value'] = $this->t('Save and continue'); + // Add a skip this step button. $form['actions']['skip'] = [ '#type' => 'submit', From d08ca889c4a8c49b27587838fd889f690b62cc0d Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 20:46:57 +0400 Subject: [PATCH 06/28] [DRUP-728] fix issue with currencies not imported --- apigee_devportal_kickstart.install | 4 ++-- src/Installer/ApigeeDevportalKickstartTasksManager.php | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index cb7ee487..95b2d92a 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -141,12 +141,12 @@ function apigee_devportal_setup_monetization(array &$install_state) { // Perform additional tasks for apigee_kickstart_m10n_add_credit. if (in_array('apigee_kickstart_m10n_add_credit', $config['modules'])) { - $operations += [ + $operations = array_merge($operations, [ [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], - ]; + ]); } $batch = [ diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php index f0707832..1a1a3b34 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManager.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -147,15 +147,23 @@ public static function createProducts(array $currencies, &$context) { /** @var \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface $currency */ foreach ($currencies as $currency) { try { + $minimum_amount = (string) $currency->getMinimumTopUpAmount(); + $currency_code = $currency->getName(); // Create a product variation for this currency. + /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */ $variation = \Drupal::entityTypeManager()->getStorage('commerce_product_variation') ->create([ 'type' => 'add_credit', 'sku' => "ADD-CREDIT-{$currency->getName()}", 'title' => $currency->getName(), 'status' => 1, - 'price' => new Price((string) $currency->getMinimumTopUpAmount(), $currency->getId()), + 'price' => new Price($minimum_amount, $currency_code), ]); + $variation->set('apigee_price_range', [ + 'minimum' => $minimum_amount, + 'default' => $minimum_amount, + 'currency_code' => $currency_code, + ]); $variation->save(); // Create an add credit product for this currency. From 6128779c2f570b11709c7230bfda04a6e73b7d0e Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:01:35 +0400 Subject: [PATCH 07/28] [DRUP-728] do not create a payment gateway for now --- apigee_devportal_kickstart.install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index 95b2d92a..74e60742 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -144,7 +144,7 @@ function apigee_devportal_setup_monetization(array &$install_state) { $operations = array_merge($operations, [ [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], - [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], +// [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], ]); } From a896224e6be27b978ae0303a712f842e0bb28898 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:09:29 +0400 Subject: [PATCH 08/28] [DRUP-728] set a maximum price for topup --- src/Installer/ApigeeDevportalKickstartTasksManager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php index 1a1a3b34..230608eb 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManager.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -161,6 +161,7 @@ public static function createProducts(array $currencies, &$context) { ]); $variation->set('apigee_price_range', [ 'minimum' => $minimum_amount, + 'maximum' => 999, 'default' => $minimum_amount, 'currency_code' => $currency_code, ]); From 435ab4a3ab71423af41296d248f7e265ebc4a2ac Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:15:14 +0400 Subject: [PATCH 09/28] [DRUP-728] update the widget type for the unit price --- ..._form_display.commerce_order_item.add_credit.add_to_cart.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml index a38fabeb..cc72a98e 100644 --- a/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml +++ b/modules/custom/apigee_kickstart_m10n_add_credit/config/install/core.entity_form_display.commerce_order_item.add_credit.add_to_cart.yml @@ -26,7 +26,7 @@ content: settings: { } third_party_settings: { } unit_price: - type: commerce_price_default + type: commerce_unit_price weight: 2 region: content settings: { } From 4e27d7b1dfec18f69cdca5fc2d6f8430e6db1303 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:37:33 +0400 Subject: [PATCH 10/28] [DRUP-728] add a dummy init batch operation --- apigee_devportal_kickstart.install | 1 + .../ApigeeDevportalKickstartTasksManager.php | 26 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index 74e60742..7464938c 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -136,6 +136,7 @@ function apigee_devportal_setup_monetization(array &$install_state) { if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) { // Add an operations to install modules. $operations = [ + [[ApigeeDevportalKickstartTasksManager::class, 'init'], [$config]], [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$config['modules']]], ]; diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php index 230608eb..b7fcb5c5 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManager.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -70,18 +70,26 @@ class ApigeeDevportalKickstartTasksManager implements ApigeeDevportalKickstartTa // $this->configFactory = $config_factory; // } + /** + * This is a dummy batch operation to show visual feedback to the user. + * + * @param $config + * An array of config. + * @param $context + * The batch context. + */ + public static function init($config, &$context) { + $context['message'] = t('Preparing setup...'); + } + /** * {@inheritdoc} */ public static function installModules(array $modules, &$context) { + sleep(2); + try { \Drupal::service('module_installer')->install($modules); - - if (!isset($context['sandbox']['progress'])) { - $context['sandbox']['progress'] = 0; - } - - $context['sandbox']['progress']++; $context['message'] = t('Installed monetization modules.'); } catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); @@ -97,7 +105,6 @@ public static function importCurrencies(array $currencies, &$context) { \Drupal::service('commerce_price.currency_importer')->import($currency->getName()); } - $context['sandbox']['progress']++; $context['message'] = t('Imported supported currencies.'); } @@ -112,7 +119,6 @@ public static function createStore(array $values, &$context) { // Save to context. $context['results']['store'] = $store; - $context['sandbox']['progress']++; $context['message'] = t('Created a default store.'); } catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); @@ -130,7 +136,6 @@ public static function createPaymentGateway(array $values, &$context) { // Save to context. $context['results']['$gateway'] = $gateway; - $context['sandbox']['progress']++; $context['message'] = t('Created a default payment gateway.'); } catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); @@ -188,8 +193,7 @@ public static function createProducts(array $currencies, &$context) { ->set('products', $add_credit_products) ->save(); - $context['sandbox']['progress']++; - $context['message'] = t('Created the default products.'); + $context['message'] = t('Created default products.'); } catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } From a24ca20a793502ff254162ef65a199df23c95052 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:46:26 +0400 Subject: [PATCH 11/28] [DRUP-728] fix phpcs notices --- .../ApigeeDevportalKickstartTasksManager.php | 73 ++++--------------- ...evportalKickstartTasksManagerInterface.php | 30 +++++--- .../Form/ApigeeEdgeConfigurationForm.php | 8 -- 3 files changed, 35 insertions(+), 76 deletions(-) diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php index b7fcb5c5..79ccf194 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManager.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -21,77 +21,31 @@ namespace Drupal\apigee_devportal_kickstart\Installer; use Drupal\apigee_m10n_add_credit\AddCreditConfig; -use Drupal\commerce_price\CurrencyImporterInterface; use Drupal\commerce_price\Price; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Extension\MissingDependencyException; -use Drupal\Core\Extension\ModuleInstallerInterface; /** * Defines a service for performing additional tasks in batch. */ class ApigeeDevportalKickstartTasksManager implements ApigeeDevportalKickstartTasksManagerInterface { -// /** -// * The entity type manager. -// * -// * @var \Drupal\Core\Entity\EntityTypeManagerInterface -// */ -// protected $entityTypeManager; -// -// /** -// * The module installer. -// * -// * @var \Drupal\Core\Extension\ModuleInstallerInterface -// */ -// protected $moduleInstaller; -// -// /** -// * The config factory. -// * -// * @var \Drupal\Core\Config\ConfigFactoryInterface -// */ -// protected $configFactory; -// -// /** -// * ApigeeDevportalKickstartTasksManager constructor. -// * -// * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager -// * The entity type manager. -// * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer -// * The module installer. -// * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory -// * The config factory. -// */ -// public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleInstallerInterface $module_installer, ConfigFactoryInterface $config_factory) { -// $this->entityTypeManager = $entity_type_manager; -// $this->moduleInstaller = $module_installer; -// $this->configFactory = $config_factory; -// } - /** - * This is a dummy batch operation to show visual feedback to the user. - * - * @param $config - * An array of config. - * @param $context - * The batch context. + * {@inheritdoc} */ - public static function init($config, &$context) { + public static function init(array $config, array &$context) { $context['message'] = t('Preparing setup...'); } /** * {@inheritdoc} */ - public static function installModules(array $modules, &$context) { + public static function installModules(array $modules, array &$context) { sleep(2); try { \Drupal::service('module_installer')->install($modules); $context['message'] = t('Installed monetization modules.'); - } catch (\Exception $exception) { + } + catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } } @@ -99,7 +53,7 @@ public static function installModules(array $modules, &$context) { /** * {@inheritdoc} */ - public static function importCurrencies(array $currencies, &$context) { + public static function importCurrencies(array $currencies, array &$context) { foreach ($currencies as $currency) { // Import the currency. \Drupal::service('commerce_price.currency_importer')->import($currency->getName()); @@ -111,7 +65,7 @@ public static function importCurrencies(array $currencies, &$context) { /** * {@inheritdoc} */ - public static function createStore(array $values, &$context) { + public static function createStore(array $values, array &$context) { try { $store = \Drupal::entityTypeManager()->getStorage('commerce_store') ->create($values); @@ -120,7 +74,8 @@ public static function createStore(array $values, &$context) { // Save to context. $context['results']['store'] = $store; $context['message'] = t('Created a default store.'); - } catch (\Exception $exception) { + } + catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } } @@ -128,7 +83,7 @@ public static function createStore(array $values, &$context) { /** * {@inheritdoc} */ - public static function createPaymentGateway(array $values, &$context) { + public static function createPaymentGateway(array $values, array &$context) { try { $gateway = \Drupal::entityTypeManager()->getStorage('commerce_payment_gateway') ->create($values); @@ -137,7 +92,8 @@ public static function createPaymentGateway(array $values, &$context) { // Save to context. $context['results']['$gateway'] = $gateway; $context['message'] = t('Created a default payment gateway.'); - } catch (\Exception $exception) { + } + catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } } @@ -145,7 +101,7 @@ public static function createPaymentGateway(array $values, &$context) { /** * {@inheritdoc} */ - public static function createProducts(array $currencies, &$context) { + public static function createProducts(array $currencies, array &$context) { if (isset($context['results']['store'])) { $add_credit_products = []; @@ -194,7 +150,8 @@ public static function createProducts(array $currencies, &$context) { ->save(); $context['message'] = t('Created default products.'); - } catch (\Exception $exception) { + } + catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } } diff --git a/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php b/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php index 99ac7a9e..a6b84a03 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManagerInterface.php @@ -25,54 +25,64 @@ */ interface ApigeeDevportalKickstartTasksManagerInterface { + /** + * This is a dummy batch operation to show visual feedback to the user. + * + * @param array $config + * An array of config. + * @param array $context + * The batch context. + */ + public static function init(array $config, array &$context); + /** * Installs the given array of modules. Used as a batch operations. * * @param array $modules * An array of module names. - * @param $context + * @param array $context * The batch context. */ - public static function installModules(array $modules, &$context); + public static function installModules(array $modules, array &$context); /** * Imports an array of currencies. * * @param \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface[] $currencies * An array of supported currencies. - * @param $context + * @param array $context * The batch context. */ - public static function importCurrencies(array $currencies, &$context); + public static function importCurrencies(array $currencies, array &$context); /** * Creates a commerce store. * * @param array $values * An array of values for the store. - * @param $context + * @param array $context * The batch context. */ - public static function createStore(array $values, &$context); + public static function createStore(array $values, array &$context); /** * Creates a payment gateway. * * @param array $values * An array of values for the gateway. - * @param $context + * @param array $context * The batch context. */ - public static function createPaymentGateway(array $values, &$context); + public static function createPaymentGateway(array $values, array &$context); /** * Creates commerce products for each supported currencies. * * @param \Apigee\Edge\Api\Monetization\Entity\SupportedCurrencyInterface[] $currencies * An array of supported currencies. - * @param $context + * @param array $context * The batch context. */ - public static function createProducts(array $currencies, &$context); + public static function createProducts(array $currencies, array &$context); } diff --git a/src/Installer/Form/ApigeeEdgeConfigurationForm.php b/src/Installer/Form/ApigeeEdgeConfigurationForm.php index cdd87bef..c667ceb1 100644 --- a/src/Installer/Form/ApigeeEdgeConfigurationForm.php +++ b/src/Installer/Form/ApigeeEdgeConfigurationForm.php @@ -112,14 +112,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->oauthTokenStorage->removeTokenFile(); } -// if ($buildInfo = $form_state->getBuildInfo()) { -// $buildInfo['args'][0]['monetization_enabled'] = \Drupal::service('apigee_devportal_kickstart.manager')->isMonetizationEnabled(); -// $form_state->setBuildInfo($buildInfo); -// } - - global $install_state; - $install_state['completed_task'] = install_verify_completed_task(); - parent::submitForm($form, $form_state); } From ebd2e9ca1bc9b022b827e241ad5573185f82f940 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 21:54:55 +0400 Subject: [PATCH 12/28] [DRUP-728] move install tasks hooks to .profile --- apigee_devportal_kickstart.install | 111 ----------------------------- apigee_devportal_kickstart.profile | 111 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 111 deletions(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index 7464938c..9075aa75 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -23,12 +23,6 @@ * Install, update and uninstall functions for Apigee Kickstart profile. */ -use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager; -use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; -use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartConfigurationForm; -use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; -use Drupal\Core\Messenger\MessengerInterface; - /** * Implements hook_install(). * @@ -82,108 +76,3 @@ function apigee_devportal_kickstart_install() { } } -/** - * Implements hook_install_tasks_alter(). - */ -function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) { - // Add tasks for configuring Apigee authentication and monetization. - $apigee_kickstart_tasks = [ - ApigeeEdgeConfigurationForm::class => [ - 'display_name' => t('Configure Apigee Edge'), - 'type' => 'form', - ], - ]; - - // The task should run before install_configure_form which creates the user. - $tasks_copy = $tasks; - $tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_kickstart_tasks + $tasks_copy; -} - -/** - * Implements hook_install_tasks(). - */ -function apigee_devportal_kickstart_install_tasks(&$install_state) { - return [ - ApigeeDevportalKickstartConfigurationForm::class => [ - 'display_name' => t('Configure kickstart'), - 'type' => 'form', - ], - 'apigee_devportal_setup_monetization' => [ - 'display_name' => t('Setup monetization'), - 'type' => 'batch', - ], - DemoInstallForm::class => [ - 'display_name' => t('Install demo content'), - 'type' => 'form', - ], - 'apigee_devportal_kickstart_theme_setup' => [ - 'display_name' => t('Install theme'), - 'display' => FALSE, - ], - ]; -} - -/** - * Install task for setting up monetization and additional modules. - * - * @param array $install_state - * The install state. - * - * @return array - * A batch definition. - */ -function apigee_devportal_setup_monetization(array &$install_state) { - if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) { - // Add an operations to install modules. - $operations = [ - [[ApigeeDevportalKickstartTasksManager::class, 'init'], [$config]], - [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$config['modules']]], - ]; - - // Perform additional tasks for apigee_kickstart_m10n_add_credit. - if (in_array('apigee_kickstart_m10n_add_credit', $config['modules'])) { - $operations = array_merge($operations, [ - [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], - [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], -// [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], - [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], - ]); - } - - $batch = [ - 'operations' => $operations, - 'title' => t('Setting up monetization'), - 'error_message' => t('The installation has encountered an error.'), - 'progress_message' => t('Completed @current out of @total tasks.'), - ]; - - return $batch; - } -} - -/** - * Install the theme. - * - * @param array $install_state - * The install state. - */ -function apigee_devportal_kickstart_theme_setup(array &$install_state) { - // Clear all status messages generated by modules installed in previous step. - Drupal::messenger()->deleteByType(MessengerInterface::TYPE_STATUS); - - // Set apigee_kickstart as the default theme. - \Drupal::configFactory() - ->getEditable('system.theme') - ->set('default', 'apigee_kickstart') - ->save(); - - // Ensure that the install profile's theme is used. - // @see _drupal_maintenance_theme() - \Drupal::service('theme.manager')->resetActiveTheme(); - - // Enable the admin theme for editing content. - \Drupal::configFactory() - ->getEditable('node.settings') - ->set('use_admin_theme', TRUE) - ->save(TRUE); -} diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index d36b61f3..8256edfb 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -23,8 +23,119 @@ * Enables modules and site configuration for apigee_devportal_kickstart. */ +use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager; +use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartConfigurationForm; +use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; +use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; use Drupal\contact\Entity\ContactForm; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; + +/** + * Implements hook_install_tasks(). + */ +function apigee_devportal_kickstart_install_tasks(&$install_state) { + return [ + ApigeeDevportalKickstartConfigurationForm::class => [ + 'display_name' => t('Configure kickstart'), + 'type' => 'form', + ], + 'apigee_devportal_setup_monetization' => [ + 'display_name' => t('Setup monetization'), + 'type' => 'batch', + ], + DemoInstallForm::class => [ + 'display_name' => t('Install demo content'), + 'type' => 'form', + ], + 'apigee_devportal_kickstart_theme_setup' => [ + 'display_name' => t('Install theme'), + 'display' => FALSE, + ], + ]; +} + +/** + * Implements hook_install_tasks_alter(). + */ +function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) { + // Add tasks for configuring Apigee authentication and monetization. + $apigee_kickstart_tasks = [ + ApigeeEdgeConfigurationForm::class => [ + 'display_name' => t('Configure Apigee Edge'), + 'type' => 'form', + ], + ]; + + // The task should run before install_configure_form which creates the user. + $tasks_copy = $tasks; + $tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_kickstart_tasks + $tasks_copy; +} + +/** + * Install task for setting up monetization and additional modules. + * + * @param array $install_state + * The install state. + * + * @return array + * A batch definition. + */ +function apigee_devportal_setup_monetization(array &$install_state) { + if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) { + // Add an operations to install modules. + $operations = [ + [[ApigeeDevportalKickstartTasksManager::class, 'init'], [$config]], + [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$config['modules']]], + ]; + + // Perform additional tasks for apigee_kickstart_m10n_add_credit. + if (in_array('apigee_kickstart_m10n_add_credit', $config['modules'])) { + $operations = array_merge($operations, [ + [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], + [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], + // [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], + [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], + ]); + } + + $batch = [ + 'operations' => $operations, + 'title' => t('Setting up monetization'), + 'error_message' => t('The installation has encountered an error.'), + 'progress_message' => t('Completed @current out of @total tasks.'), + ]; + + return $batch; + } +} + +/** + * Install the theme. + * + * @param array $install_state + * The install state. + */ +function apigee_devportal_kickstart_theme_setup(array &$install_state) { + // Clear all status messages generated by modules installed in previous step. + Drupal::messenger()->deleteByType(MessengerInterface::TYPE_STATUS); + + // Set apigee_kickstart as the default theme. + \Drupal::configFactory() + ->getEditable('system.theme') + ->set('default', 'apigee_kickstart') + ->save(); + + // Ensure that the install profile's theme is used. + // @see _drupal_maintenance_theme() + \Drupal::service('theme.manager')->resetActiveTheme(); + + // Enable the admin theme for editing content. + \Drupal::configFactory() + ->getEditable('node.settings') + ->set('use_admin_theme', TRUE) + ->save(TRUE); +} /** * Implements hook_form_FORM_ID_alter() for install_configure_form(). From fdaf61008aa75efccd00343f14a496e4eb426b50 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 22:10:36 +0400 Subject: [PATCH 13/28] [DRUP-728] show the warning message on the edge form as an info message --- apigee_devportal_kickstart.libraries.yml | 5 ++++ ...e_devportal_kickstart.apigee_edge_form.css | 23 +++++++++++++++++++ resources/apigee/questionmark-disc.svg | 1 + .../Form/ApigeeEdgeConfigurationForm.php | 2 ++ 4 files changed, 31 insertions(+) create mode 100644 apigee_devportal_kickstart.libraries.yml create mode 100644 css/apigee_devportal_kickstart.apigee_edge_form.css create mode 100644 resources/apigee/questionmark-disc.svg diff --git a/apigee_devportal_kickstart.libraries.yml b/apigee_devportal_kickstart.libraries.yml new file mode 100644 index 00000000..c288deec --- /dev/null +++ b/apigee_devportal_kickstart.libraries.yml @@ -0,0 +1,5 @@ +apigee_edge_form: + version: VERSION + css: + theme: + css/apigee_devportal_kickstart.apigee_edge_form.css: {} diff --git a/css/apigee_devportal_kickstart.apigee_edge_form.css b/css/apigee_devportal_kickstart.apigee_edge_form.css new file mode 100644 index 00000000..8c9d72e0 --- /dev/null +++ b/css/apigee_devportal_kickstart.apigee_edge_form.css @@ -0,0 +1,23 @@ +/* + * Copyright 2018 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. + */ +.apigee-edge-authentication-form .messages--warning { + border-color: #0074bd; + box-shadow: -8px 0 0 #0074bd; + background-color: #e6f5ff; + background-image: url(../resources/apigee/questionmark-disc.svg); + color: #666666; +} diff --git a/resources/apigee/questionmark-disc.svg b/resources/apigee/questionmark-disc.svg new file mode 100644 index 00000000..1363940d --- /dev/null +++ b/resources/apigee/questionmark-disc.svg @@ -0,0 +1 @@ + diff --git a/src/Installer/Form/ApigeeEdgeConfigurationForm.php b/src/Installer/Form/ApigeeEdgeConfigurationForm.php index c667ceb1..160058ee 100644 --- a/src/Installer/Form/ApigeeEdgeConfigurationForm.php +++ b/src/Installer/Form/ApigeeEdgeConfigurationForm.php @@ -67,6 +67,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#limit_validation_errors' => [], ]; + $form['#attached']['library'][] = 'apigee_devportal_kickstart/apigee_edge_form'; + return $form; } From 34d444b283c948c769c23ce1998a37c8547512c4 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 23:11:50 +0400 Subject: [PATCH 14/28] [DRUP-728] remove address module as a dependency --- apigee_devportal_kickstart.info.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/apigee_devportal_kickstart.info.yml b/apigee_devportal_kickstart.info.yml index c9b73a2f..724e3f7f 100644 --- a/apigee_devportal_kickstart.info.yml +++ b/apigee_devportal_kickstart.info.yml @@ -56,7 +56,6 @@ install: - admin_toolbar_links_access_filter - admin_toolbar_tools - adminimal_admin_toolbar -- address - apigee_edge - apigee_edge_apidocs - apigee_kickstart_customizer From 3853fe2ecd4ea169e66f469cc5e3fb1b45f88692 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 23:12:15 +0400 Subject: [PATCH 15/28] [DRUP-728] remove sleep function --- apigee_devportal_kickstart.install | 1 - src/Installer/ApigeeDevportalKickstartTasksManager.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/apigee_devportal_kickstart.install b/apigee_devportal_kickstart.install index 9075aa75..2a887034 100644 --- a/apigee_devportal_kickstart.install +++ b/apigee_devportal_kickstart.install @@ -75,4 +75,3 @@ function apigee_devportal_kickstart_install() { } } } - diff --git a/src/Installer/ApigeeDevportalKickstartTasksManager.php b/src/Installer/ApigeeDevportalKickstartTasksManager.php index 79ccf194..ec374e02 100644 --- a/src/Installer/ApigeeDevportalKickstartTasksManager.php +++ b/src/Installer/ApigeeDevportalKickstartTasksManager.php @@ -39,8 +39,6 @@ public static function init(array $config, array &$context) { * {@inheritdoc} */ public static function installModules(array $modules, array &$context) { - sleep(2); - try { \Drupal::service('module_installer')->install($modules); $context['message'] = t('Installed monetization modules.'); From 6fb5791596b5a368384967acaef36a5cf2f4f7c5 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 23:12:36 +0400 Subject: [PATCH 16/28] [DRUP-728] add m10n tasks if m10n modules are available on the system --- apigee_devportal_kickstart.profile | 91 ++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 16 deletions(-) diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 8256edfb..49936422 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -28,6 +28,7 @@ use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartCon use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; use Drupal\contact\Entity\ContactForm; +use Drupal\Core\Extension\InfoParserException; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; @@ -35,15 +36,7 @@ use Drupal\Core\Messenger\MessengerInterface; * Implements hook_install_tasks(). */ function apigee_devportal_kickstart_install_tasks(&$install_state) { - return [ - ApigeeDevportalKickstartConfigurationForm::class => [ - 'display_name' => t('Configure kickstart'), - 'type' => 'form', - ], - 'apigee_devportal_setup_monetization' => [ - 'display_name' => t('Setup monetization'), - 'type' => 'batch', - ], + $tasks = [ DemoInstallForm::class => [ 'display_name' => t('Install demo content'), 'type' => 'form', @@ -53,6 +46,23 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { 'display' => FALSE, ], ]; + + // Add monetization tasks if the apigee_m10n modules are available. + if (_apigee_devportal_kickstart_is_monetizable()) { + $tasks = array_merge([ + 'apigee_devportal_monetization_preflight' => [], + ApigeeDevportalKickstartConfigurationForm::class => [ + 'display_name' => t('Configure kickstart'), + 'type' => 'form', + ], + 'apigee_devportal_setup_monetization' => [ + 'display_name' => t('Setup monetization'), + 'type' => 'batch', + ], + ], $tasks); + } + + return $tasks; } /** @@ -72,6 +82,23 @@ function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) $tasks = array_slice($tasks_copy, 0, array_search('install_configure_form', array_keys($tasks))) + $apigee_kickstart_tasks + $tasks_copy; } +/** + * Prepares profile for monetization setup. + * + * @param array $install_state + * The install state. + */ +function apigee_devportal_monetization_preflight(array &$install_state) { + // The monetization configuration form needs an address field. + // Enable the address module. + try { + \Drupal::service('module_installer')->install(['address']); + } + catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } +} + /** * Install task for setting up monetization and additional modules. * @@ -85,17 +112,31 @@ function apigee_devportal_setup_monetization(array &$install_state) { if (isset($install_state['m10n_config']) && ($config = $install_state['m10n_config'])) { // Add an operations to install modules. $operations = [ - [[ApigeeDevportalKickstartTasksManager::class, 'init'], [$config]], - [[ApigeeDevportalKickstartTasksManager::class, 'installModules'], [$config['modules']]], + [ + [ApigeeDevportalKickstartTasksManager::class, 'init'], + [$config] + ], + [ + [ApigeeDevportalKickstartTasksManager::class, 'installModules'], + [$config['modules']], + ], ]; // Perform additional tasks for apigee_kickstart_m10n_add_credit. if (in_array('apigee_kickstart_m10n_add_credit', $config['modules'])) { $operations = array_merge($operations, [ - [[ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], [$config['supported_currencies']]], - [[ApigeeDevportalKickstartTasksManager::class, 'createStore'], [$config['store']]], - // [[ApigeeDevportalKickstartTasksManager::class, 'createPaymentGateway'], [$config['gateway']]], - [[ApigeeDevportalKickstartTasksManager::class, 'createProducts'], [$config['supported_currencies']]], + [ + [ApigeeDevportalKickstartTasksManager::class, 'importCurrencies'], + [$config['supported_currencies']], + ], + [ + [ApigeeDevportalKickstartTasksManager::class, 'createStore'], + [$config['store']], + ], + [ + [ApigeeDevportalKickstartTasksManager::class, 'createProducts'], + [$config['supported_currencies']], + ], ]); } @@ -151,5 +192,23 @@ function apigee_devportal_kickstart_form_install_configure_form_alter(&$form, Fo */ function apigee_devportal_kickstart_form_install_configure_submit($form, FormStateInterface $form_state) { $site_mail = $form_state->getValue('site_mail'); - ContactForm::load('feedback')->setRecipients([$site_mail])->trustData()->save(); + ContactForm::load('feedback') + ->setRecipients([$site_mail]) + ->trustData() + ->save(); +} + +/** + * Helper to check if monetization can be enabled for kickstart. + */ +function _apigee_devportal_kickstart_is_monetizable() { + try { + $modules = \Drupal::service('extension.list.module')->getList(); + return isset($modules['apigee_m10n']) && isset($modules['apigee_m10n_add_credit']); + } + catch (InfoParserException $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + + return FALSE; } From 58f99a4d30428a12d6c54dc9ec634cf9cfb58878 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Fri, 19 Apr 2019 23:22:39 +0400 Subject: [PATCH 17/28] [DRUP-728] remove apigee_m10n and dependencies --- composer.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/composer.json b/composer.json index 96188d36..6e9152af 100644 --- a/composer.json +++ b/composer.json @@ -33,9 +33,7 @@ "drupal/admin_toolbar": "^1.0", "drupal/adminimal_admin_toolbar": "^1.9", "drupal/apigee_edge": "1.x-dev", - "drupal/apigee_m10n": "1.x-dev", "drupal/better_exposed_filters": "^3.0@alpha", - "drupal/commerce": "^2.13", "drupal/core": "^8.6.0", "drupal/default_content": "^1.0@alpha", "drupal/fontawesome": "^2.12", From abaff2f66886a3b652bb9777911c2f60f5d7cc86 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Sat, 20 Apr 2019 00:30:17 +0400 Subject: [PATCH 18/28] [DRUP-728] fix issue with supported_currencies array --- .../Form/ApigeeDevportalKickstartConfigurationForm.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php index 1b78465b..d82675da 100644 --- a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php +++ b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php @@ -324,7 +324,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) { if (count($values['modules'])) { // Update the supported currencies. if (isset($values['supported_currencies'])) { - foreach ($values['supported_currencies'] as $currency_code) { + $supported_currencies = array_keys(array_filter($values['supported_currencies'])); + $values['supported_currencies'] = []; + foreach ($supported_currencies as $currency_code) { $values['supported_currencies'][$currency_code] = $this->supportedCurrencies[strtolower($currency_code)]; } } From 4ca1ed89377584531a3d54afbad73ee1c1c656cf Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Sat, 20 Apr 2019 01:09:38 +0400 Subject: [PATCH 19/28] [DRUP-728] remove debug route --- apigee_devportal_kickstart.routing.yml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 apigee_devportal_kickstart.routing.yml diff --git a/apigee_devportal_kickstart.routing.yml b/apigee_devportal_kickstart.routing.yml deleted file mode 100644 index 33cafd33..00000000 --- a/apigee_devportal_kickstart.routing.yml +++ /dev/null @@ -1,7 +0,0 @@ -kickstart.debug: - path: '/admin/config/m10n' - defaults: - _form: '\Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeM10nConfigurationForm' - _title: 'Configure m10n' - requirements: - _permission: 'administer site' From affa78cc78957a2d1c59814bb8eb368a15f65465 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Sat, 20 Apr 2019 01:29:57 +0400 Subject: [PATCH 20/28] Revert "[DRUP-728] remove debug route" This reverts commit 4ca1ed89377584531a3d54afbad73ee1c1c656cf. --- apigee_devportal_kickstart.routing.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 apigee_devportal_kickstart.routing.yml diff --git a/apigee_devportal_kickstart.routing.yml b/apigee_devportal_kickstart.routing.yml new file mode 100644 index 00000000..33cafd33 --- /dev/null +++ b/apigee_devportal_kickstart.routing.yml @@ -0,0 +1,7 @@ +kickstart.debug: + path: '/admin/config/m10n' + defaults: + _form: '\Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeM10nConfigurationForm' + _title: 'Configure m10n' + requirements: + _permission: 'administer site' From 0aa4b3a9649f1d35af0a5bda88348d0047eb7b9f Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 17:22:46 +0400 Subject: [PATCH 21/28] [DRUP-728] replace address field with custom fields --- ...geeDevportalKickstartConfigurationForm.php | 100 ++++++++++++++---- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php index d82675da..ee3d0162 100644 --- a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php +++ b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php @@ -24,8 +24,10 @@ use Apigee\Edge\Api\Monetization\Controller\OrganizationProfileController; use Apigee\Edge\Api\Monetization\Controller\SupportedCurrencyController; use CommerceGuys\Addressing\AddressFormat\AddressField; -use CommerceGuys\Addressing\AddressFormat\FieldOverride; +use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface; use CommerceGuys\Intl\Currency\CurrencyRepository; +use Drupal\address\FieldHelper; +use Drupal\address\LabelHelper; use Drupal\apigee_edge\SDKConnectorInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; @@ -59,6 +61,13 @@ class ApigeeDevportalKickstartConfigurationForm extends FormBase { */ protected $languageManager; + /** + * The subdivision repository. + * + * @var \CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface + */ + protected $subdivisionRepository; + /** * The Apigee Organization. * @@ -87,10 +96,13 @@ class ApigeeDevportalKickstartConfigurationForm extends FormBase { * SDK connector service. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface $subdivision_repository + * The subdivision repository. */ - public function __construct(SDKConnectorInterface $sdk_connector, LanguageManagerInterface $language_manager) { + public function __construct(SDKConnectorInterface $sdk_connector, LanguageManagerInterface $language_manager, SubdivisionRepositoryInterface $subdivision_repository) { $this->sdkConnector = $sdk_connector; $this->languageManager = $language_manager; + $this->subdivisionRepository = $subdivision_repository; $this->currencyRepository = new CurrencyRepository(); try { @@ -116,7 +128,6 @@ public function __construct(SDKConnectorInterface $sdk_connector, LanguageManage catch (\Exception $exception) { watchdog_exception('apigee_kickstart', $exception); } - } /** @@ -125,7 +136,8 @@ public function __construct(SDKConnectorInterface $sdk_connector, LanguageManage public static function create(ContainerInterface $container) { return new static( $container->get('apigee_edge.sdk_connector'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('address.subdivision_repository') ); } @@ -240,30 +252,62 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => 'online', ]; + // Create individual address fields. + // We cannot use the address field because #ajax forms won't work in the + // installer. $form['store']['address'] = [ - '#title' => $this->t('Address'), - '#type' => 'address', - '#field_overrides' => [ - AddressField::GIVEN_NAME => FieldOverride::HIDDEN, - AddressField::FAMILY_NAME => FieldOverride::HIDDEN, - AddressField::ORGANIZATION => FieldOverride::HIDDEN, - AddressField::ADDRESS_LINE2 => FieldOverride::HIDDEN, - AddressField::POSTAL_CODE => FieldOverride::OPTIONAL, + '#type' => 'tree', + ]; + + $form['store']['address']['country_code'] = [ + '#title' => $this->t('Country'), + '#type' => 'address_country', + '#required' => TRUE, + ]; + + $address_fields = [ + AddressField::ADDRESS_LINE1 => [ + 'size' => 60, + 'placeholder' => 'Acme Street', + ], + AddressField::ADDRESS_LINE2 => [ + 'size' => 60, + 'placeholder' => '', + ], + AddressField::LOCALITY => [ + 'size' => 30, + 'placeholder' => 'Santa Clara', + ], + AddressField::ADMINISTRATIVE_AREA => [ + 'size' => 30, + 'placeholder' => 'CA', + ], + AddressField::POSTAL_CODE => [ + 'size' => 10, + 'placeholder' => '95050', ], ]; + $labels = LabelHelper::getGenericFieldLabels(); + foreach ($address_fields as $address_field => $settings) { + $form['store']['address'][FieldHelper::getPropertyName($address_field)] = [ + '#title' => $labels[$address_field], + '#type' => 'textfield', + '#size' => $settings['size'], + '#placeholder' => $settings['placeholder'], + ]; + } // Add default address from organization. if ($addresses = $this->organization->getAddresses()) { /** @var \Apigee\Edge\Api\Monetization\Structure\Address $address */ $address = reset($addresses); - $form['store']['address']['#default_value'] = [ - 'address_line1' => $address->getAddress1(), - 'locality' => $address->getCity(), - 'administrative_area' => $address->getState(), - 'country_code' => $address->getCountry(), - 'postal_code' => $address->getZip(), - ]; + $form['store']['address']['country_code']['#default_value'] = $address->getCountry(); + $form['store']['address'][FieldHelper::getPropertyName(AddressField::ADDRESS_LINE1)]['#default_value'] = $address->getAddress1(); + $form['store']['address'][FieldHelper::getPropertyName(AddressField::ADDRESS_LINE1)]['#default_value'] = $address->getAddress1(); + $form['store']['address'][FieldHelper::getPropertyName(AddressField::LOCALITY)]['#default_value'] = $address->getCity(); + $form['store']['address'][FieldHelper::getPropertyName(AddressField::ADMINISTRATIVE_AREA)]['#default_value'] = $address->getState(); + $form['store']['address'][FieldHelper::getPropertyName(AddressField::POSTAL_CODE)]['#default_value'] = $address->getZip(); } if (count($this->supportedCurrencies)) { @@ -313,6 +357,24 @@ public function buildForm(array $form, FormStateInterface $form_state) { return $form; } + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + + // Validate administrative area. + if (($store = $form_state->getValue('store')) + && ($address = $store['address']) + && ($property_name = FieldHelper::getPropertyName(AddressField::ADMINISTRATIVE_AREA)) + && isset($address[$property_name]) + && ($subdivisions = $this->subdivisionRepository->getList([$address['country_code']])) + && !isset($subdivisions[strtoupper($address[$property_name])]) + ) { + $form_state->setErrorByName('address][' . $property_name, $this->t('Please enter a valid administrative area.')); + } + } + /** * {@inheritdoc} */ From ea8210465f85e263281dfdfa9168643951957fd6 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 17:23:15 +0400 Subject: [PATCH 22/28] [DRUP-728] remove debug routing file --- apigee_devportal_kickstart.routing.yml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 apigee_devportal_kickstart.routing.yml diff --git a/apigee_devportal_kickstart.routing.yml b/apigee_devportal_kickstart.routing.yml deleted file mode 100644 index 33cafd33..00000000 --- a/apigee_devportal_kickstart.routing.yml +++ /dev/null @@ -1,7 +0,0 @@ -kickstart.debug: - path: '/admin/config/m10n' - defaults: - _form: '\Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeM10nConfigurationForm' - _title: 'Configure m10n' - requirements: - _permission: 'administer site' From 8f9b2dfb4f104acec385a47a87b461f571139f1f Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 17:24:08 +0400 Subject: [PATCH 23/28] [DRUP-728] Remove gateway default values --- .../Form/ApigeeDevportalKickstartConfigurationForm.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php index ee3d0162..40eb0697 100644 --- a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php +++ b/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php @@ -393,14 +393,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } - // Add values for a payment gateway. - // TODO: Figure out if this should be configurable in the form. - $values['gateway'] = [ - 'id' => 'default', - 'label' => 'Default', - 'plugin' => 'manual', - ]; - // Save to install state. $buildInfo = $form_state->getBuildInfo(); $buildInfo['args'][0]['m10n_config'] = $values; From b9e2d67460604bed60bd723b9be82566d4bd391f Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 17:42:06 +0400 Subject: [PATCH 24/28] [DRUP-728] Show the monetization tasks only if org is monetizable --- apigee_devportal_kickstart.profile | 2 +- apigee_devportal_kickstart.services.yml | 3 ++ .../ApigeeDevportalKickstartMonetization.php | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 apigee_devportal_kickstart.services.yml create mode 100644 src/Installer/ApigeeDevportalKickstartMonetization.php diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 49936422..1c2a293e 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -204,7 +204,7 @@ function apigee_devportal_kickstart_form_install_configure_submit($form, FormSta function _apigee_devportal_kickstart_is_monetizable() { try { $modules = \Drupal::service('extension.list.module')->getList(); - return isset($modules['apigee_m10n']) && isset($modules['apigee_m10n_add_credit']); + return Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable() && isset($modules['apigee_m10n']) && isset($modules['apigee_m10n_add_credit']); } catch (InfoParserException $exception) { watchdog_exception('apigee_kickstart', $exception); diff --git a/apigee_devportal_kickstart.services.yml b/apigee_devportal_kickstart.services.yml new file mode 100644 index 00000000..b4a66d0c --- /dev/null +++ b/apigee_devportal_kickstart.services.yml @@ -0,0 +1,3 @@ +services: + apigee_devportal_kickstart.monetization: + class: Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartMonetization diff --git a/src/Installer/ApigeeDevportalKickstartMonetization.php b/src/Installer/ApigeeDevportalKickstartMonetization.php new file mode 100644 index 00000000..b94206c2 --- /dev/null +++ b/src/Installer/ApigeeDevportalKickstartMonetization.php @@ -0,0 +1,42 @@ +getOrganization(); + $client = \Drupal::service('apigee_edge.sdk_connector')->getClient(); + $organization_controller = new OrganizationController($client); + $organization = $organization_controller->load($organization_id); + return $organization->getPropertyValue('features.isMonetizationEnabled') === 'true'; + } + catch (\Exception $exception) { + watchdog_exception('apigee_kickstart', $exception); + } + + return FALSE; + } +} From 054f55b7b8e0ea88a810972f50483f53a88b130b Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 20:09:49 +0400 Subject: [PATCH 25/28] [DRUP-728] Refactor monetization check --- apigee_devportal_kickstart.profile | 43 +++++--------- apigee_devportal_kickstart.services.yml | 1 + .../ApigeeDevportalKickstartMonetization.php | 56 ++++++++++++++++--- 3 files changed, 64 insertions(+), 36 deletions(-) diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 1c2a293e..02a42d53 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -47,19 +47,20 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { ], ]; - // Add monetization tasks if the apigee_m10n modules are available. - if (_apigee_devportal_kickstart_is_monetizable()) { - $tasks = array_merge([ - 'apigee_devportal_monetization_preflight' => [], - ApigeeDevportalKickstartConfigurationForm::class => [ - 'display_name' => t('Configure kickstart'), - 'type' => 'form', - ], - 'apigee_devportal_setup_monetization' => [ - 'display_name' => t('Setup monetization'), - 'type' => 'batch', - ], - ], $tasks); + // Add monetization tasks if the configured organization is monetizable. + if (Drupal::moduleHandler()->moduleExists('apigee_edge') && Drupal::hasService('apigee_devportal_kickstart.monetization') && Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { +// if (Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { + $tasks = array_merge([ + ApigeeDevportalKickstartConfigurationForm::class => [ + 'display_name' => t('Configure kickstart'), + 'type' => 'form', + ], + 'apigee_devportal_setup_monetization' => [ + 'display_name' => t('Setup monetization'), + 'type' => 'batch', + ], + ], $tasks); +// } } return $tasks; @@ -75,6 +76,7 @@ function apigee_devportal_kickstart_install_tasks_alter(&$tasks, $install_state) 'display_name' => t('Configure Apigee Edge'), 'type' => 'form', ], + 'apigee_devportal_monetization_preflight' => [], ]; // The task should run before install_configure_form which creates the user. @@ -197,18 +199,3 @@ function apigee_devportal_kickstart_form_install_configure_submit($form, FormSta ->trustData() ->save(); } - -/** - * Helper to check if monetization can be enabled for kickstart. - */ -function _apigee_devportal_kickstart_is_monetizable() { - try { - $modules = \Drupal::service('extension.list.module')->getList(); - return Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable() && isset($modules['apigee_m10n']) && isset($modules['apigee_m10n_add_credit']); - } - catch (InfoParserException $exception) { - watchdog_exception('apigee_kickstart', $exception); - } - - return FALSE; -} diff --git a/apigee_devportal_kickstart.services.yml b/apigee_devportal_kickstart.services.yml index b4a66d0c..3576d562 100644 --- a/apigee_devportal_kickstart.services.yml +++ b/apigee_devportal_kickstart.services.yml @@ -1,3 +1,4 @@ services: apigee_devportal_kickstart.monetization: class: Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartMonetization + # We cannot pass constructor arguments here because dependent services do not exist at this point. diff --git a/src/Installer/ApigeeDevportalKickstartMonetization.php b/src/Installer/ApigeeDevportalKickstartMonetization.php index b94206c2..36c16815 100644 --- a/src/Installer/ApigeeDevportalKickstartMonetization.php +++ b/src/Installer/ApigeeDevportalKickstartMonetization.php @@ -22,21 +22,61 @@ use Apigee\Edge\Api\Management\Controller\OrganizationController; +/** + * Defines a service to handle monetization tasks. + */ class ApigeeDevportalKickstartMonetization { - public function isMonetizable() { + /** + * Determines if the configured organization is monetizable. + * + * @return bool + * TRUE if organization is monetizable. + */ + public static function isMonetizable() { try { - $organization_id = \Drupal::service('apigee_edge.sdk_connector') - ->getOrganization(); - $client = \Drupal::service('apigee_edge.sdk_connector')->getClient(); - $organization_controller = new OrganizationController($client); - $organization = $organization_controller->load($organization_id); - return $organization->getPropertyValue('features.isMonetizationEnabled') === 'true'; + $sdk_connector = \Drupal::service('apigee_edge.sdk_connector'); + $organization_controller = new OrganizationController($sdk_connector->getClient()); + $organization = $organization_controller->load($sdk_connector->getOrganization()); + return ($organization->getPropertyValue('features.isMonetizationEnabled') === 'true'); } catch (\Exception $exception) { - watchdog_exception('apigee_kickstart', $exception); + // Do not log the exception here. This litters the logs since this is run + // before each install tasks. } return FALSE; } + + /** + * Check if all dependencies are met. + * + * @return bool + * TRUE if all dependencies are met. FALSE otherwise. + */ + protected static function meetsDependencies(): bool { + $extension_list = \Drupal::service('extension.list.module'); + foreach (static::dependencies() as $dependency) { + if (!$extension_list->exists($dependency)) { + return FALSE; + } + } + + return TRUE; + } + + /** + * Returns an array of module names required for monetization. + * + * @return array + */ + protected static function dependencies(): array { + return [ + 'address', + 'apigee_m10n', + 'apigee_m10n_add_credit', + 'commerce', + ]; + } + } From 67a6d9d1840c19e084dc146719a1778860747860 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 20:24:33 +0400 Subject: [PATCH 26/28] [DRUP-728] Fix phpcs warnings --- apigee_devportal_kickstart.profile | 22 +++++++++---------- .../ApigeeDevportalKickstartMonetization.php | 3 ++- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 02a42d53..48d4ebe8 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -49,18 +49,16 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { // Add monetization tasks if the configured organization is monetizable. if (Drupal::moduleHandler()->moduleExists('apigee_edge') && Drupal::hasService('apigee_devportal_kickstart.monetization') && Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { -// if (Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { - $tasks = array_merge([ - ApigeeDevportalKickstartConfigurationForm::class => [ - 'display_name' => t('Configure kickstart'), - 'type' => 'form', - ], - 'apigee_devportal_setup_monetization' => [ - 'display_name' => t('Setup monetization'), - 'type' => 'batch', - ], - ], $tasks); -// } + $tasks = array_merge([ + ApigeeDevportalKickstartConfigurationForm::class => [ + 'display_name' => t('Configure kickstart'), + 'type' => 'form', + ], + 'apigee_devportal_setup_monetization' => [ + 'display_name' => t('Setup monetization'), + 'type' => 'batch', + ], + ], $tasks); } return $tasks; diff --git a/src/Installer/ApigeeDevportalKickstartMonetization.php b/src/Installer/ApigeeDevportalKickstartMonetization.php index 36c16815..7dd42e1b 100644 --- a/src/Installer/ApigeeDevportalKickstartMonetization.php +++ b/src/Installer/ApigeeDevportalKickstartMonetization.php @@ -52,7 +52,7 @@ public static function isMonetizable() { * Check if all dependencies are met. * * @return bool - * TRUE if all dependencies are met. FALSE otherwise. + * TRUE if all dependencies are met. FALSE otherwise. */ protected static function meetsDependencies(): bool { $extension_list = \Drupal::service('extension.list.module'); @@ -69,6 +69,7 @@ protected static function meetsDependencies(): bool { * Returns an array of module names required for monetization. * * @return array + * An array of module names. */ protected static function dependencies(): array { return [ From b478e920634f236e81982fded8c1cd5a5269e289 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 20:29:23 +0400 Subject: [PATCH 27/28] [DRUP-728] Rename the configuration form --- apigee_devportal_kickstart.profile | 6 +++--- ...igeeDevportalKickstartMonetizationConfigurationForm.php} | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) rename src/Installer/Form/{ApigeeDevportalKickstartConfigurationForm.php => ApigeeDevportalKickstartMonetizationConfigurationForm.php} (98%) diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 48d4ebe8..464e1851 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -24,7 +24,7 @@ */ use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager; -use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartConfigurationForm; +use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeDevportalKickstartMonetizationConfigurationForm; use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm; use Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm; use Drupal\contact\Entity\ContactForm; @@ -50,8 +50,8 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { // Add monetization tasks if the configured organization is monetizable. if (Drupal::moduleHandler()->moduleExists('apigee_edge') && Drupal::hasService('apigee_devportal_kickstart.monetization') && Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { $tasks = array_merge([ - ApigeeDevportalKickstartConfigurationForm::class => [ - 'display_name' => t('Configure kickstart'), + ApigeeDevportalKickstartMonetizationConfigurationForm::class => [ + 'display_name' => t('Configure monetization'), 'type' => 'form', ], 'apigee_devportal_setup_monetization' => [ diff --git a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php b/src/Installer/Form/ApigeeDevportalKickstartMonetizationConfigurationForm.php similarity index 98% rename from src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php rename to src/Installer/Form/ApigeeDevportalKickstartMonetizationConfigurationForm.php index 40eb0697..994a25f2 100644 --- a/src/Installer/Form/ApigeeDevportalKickstartConfigurationForm.php +++ b/src/Installer/Form/ApigeeDevportalKickstartMonetizationConfigurationForm.php @@ -38,7 +38,7 @@ /** * Configuration form for Apigee Monetization. */ -class ApigeeDevportalKickstartConfigurationForm extends FormBase { +class ApigeeDevportalKickstartMonetizationConfigurationForm extends FormBase { /** * SDK connector service. @@ -145,7 +145,7 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function getFormId() { - return 'apigee_devportal_kickstart_configuration_form'; + return 'apigee_devportal_kickstart_m10n_configuration_form'; } /** @@ -155,7 +155,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Some messages stick around for installation tasks. Clear them all. $this->messenger()->deleteAll(); - $form['#title'] = $this->t('Configure Apigee Kickstart'); + $form['#title'] = $this->t('Configure monetization'); $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = [ From 3fcf141046cb9ca05f6bc478e60279417f34fdf5 Mon Sep 17 00:00:00 2001 From: Arshad Chummun Date: Thu, 25 Apr 2019 20:53:29 +0400 Subject: [PATCH 28/28] [DRUP-728] Check for the address subdivision repository service --- apigee_devportal_kickstart.profile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apigee_devportal_kickstart.profile b/apigee_devportal_kickstart.profile index 464e1851..bbce27a0 100644 --- a/apigee_devportal_kickstart.profile +++ b/apigee_devportal_kickstart.profile @@ -48,7 +48,9 @@ function apigee_devportal_kickstart_install_tasks(&$install_state) { ]; // Add monetization tasks if the configured organization is monetizable. - if (Drupal::moduleHandler()->moduleExists('apigee_edge') && Drupal::hasService('apigee_devportal_kickstart.monetization') && Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { + if (Drupal::moduleHandler()->moduleExists('apigee_edge') + && Drupal::hasService('address.subdivision_repository') + && Drupal::hasService('apigee_devportal_kickstart.monetization') && Drupal::service('apigee_devportal_kickstart.monetization')->isMonetizable()) { $tasks = array_merge([ ApigeeDevportalKickstartMonetizationConfigurationForm::class => [ 'display_name' => t('Configure monetization'),