Skip to content

Commit

Permalink
Merge pull request #88 from arshad/DRUP-728-m10n-kickstart
Browse files Browse the repository at this point in the history
[DRUP-728] Apigee Monetization for Kickstart
  • Loading branch information
jacine committed Apr 26, 2019
2 parents 644780c + 3fcf141 commit 1dcaf8c
Show file tree
Hide file tree
Showing 23 changed files with 1,275 additions and 66 deletions.
65 changes: 0 additions & 65 deletions apigee_devportal_kickstart.install
Expand Up @@ -23,9 +23,6 @@
* Install, update and uninstall functions for Apigee Kickstart profile.
*/

use Drupal\apigee_devportal_kickstart\Installer\Form\ApigeeEdgeConfigurationForm;
use Drupal\Core\Messenger\MessengerInterface;

/**
* Implements hook_install().
*
Expand Down Expand Up @@ -78,65 +75,3 @@ 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' => [
'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;
}

/**
* Implements hook_install_tasks().
*/
function apigee_devportal_kickstart_install_tasks(&$install_state) {
$tasks = [
'\Drupal\apigee_devportal_kickstart\Installer\Form\DemoInstallForm' => [
'display_name' => t('Install demo content'),
'type' => 'form',
],
'apigee_devportal_kickstart_theme_setup' => [
'display_name' => t('Install theme'),
'display' => FALSE,
],
];
return $tasks;
}

/**
* 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);
}
5 changes: 5 additions & 0 deletions apigee_devportal_kickstart.libraries.yml
@@ -0,0 +1,5 @@
apigee_edge_form:
version: VERSION
css:
theme:
css/apigee_devportal_kickstart.apigee_edge_form.css: {}
159 changes: 158 additions & 1 deletion apigee_devportal_kickstart.profile
Expand Up @@ -23,8 +23,162 @@
* Enables modules and site configuration for apigee_devportal_kickstart.
*/

use Drupal\apigee_devportal_kickstart\Installer\ApigeeDevportalKickstartTasksManager;
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;
use Drupal\Core\Extension\InfoParserException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
* Implements hook_install_tasks().
*/
function apigee_devportal_kickstart_install_tasks(&$install_state) {
$tasks = [
DemoInstallForm::class => [
'display_name' => t('Install demo content'),
'type' => 'form',
],
'apigee_devportal_kickstart_theme_setup' => [
'display_name' => t('Install theme'),
'display' => FALSE,
],
];

// Add monetization tasks if the configured organization is monetizable.
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'),
'type' => 'form',
],
'apigee_devportal_setup_monetization' => [
'display_name' => t('Setup monetization'),
'type' => 'batch',
],
], $tasks);
}

return $tasks;
}

/**
* 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',
],
'apigee_devportal_monetization_preflight' => [],
];

// 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;
}

/**
* 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.
*
* @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, '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().
Expand All @@ -40,5 +194,8 @@ 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();
}
4 changes: 4 additions & 0 deletions apigee_devportal_kickstart.services.yml
@@ -0,0 +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.
23 changes: 23 additions & 0 deletions 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;
}
@@ -0,0 +1,8 @@
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
- apigee_m10n_add_credit:apigee_m10n_add_credit
@@ -0,0 +1,9 @@
langcode: en
status: true
dependencies: { }
label: 'Add credit'
id: add_credit
purchasableEntityType: commerce_product_variation
orderType: default
traits: { }
locked: false
@@ -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
@@ -0,0 +1,9 @@
langcode: en
status: true
dependencies: { }
id: add_credit
label: 'Add credit'
orderItemType: add_credit
generateTitle: false
traits: { }
locked: false
@@ -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_unit_price
weight: 2
region: content
settings: { }
third_party_settings: { }
hidden:
created: true
quantity: true

0 comments on commit 1dcaf8c

Please sign in to comment.