Newer
Older

Rajab Natshah
committed
/**
* @file
* Enables modules and site configuration for a Varbase site installation.
*/
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Form\FormStateInterface;

Rajab Natshah
committed
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\varbase\Config\ConfigBit;

Rajab Natshah
committed
use Drupal\varbase\Form\ConfigureMultilingualForm;
use Drupal\varbase\Form\AssemblerForm;
use Drupal\varbase\Form\DevelopmentToolsAssemblerForm;

Rajab Natshah
committed
use Drupal\varbase\Entity\VarbaseEntityDefinitionUpdateManager;
/**
* Implements hook_form_FORM_ID_alter() for install_configure_form().
*
* Allows the profile to alter the site configuration form.
*/
function varbase_form_install_configure_form_alter(&$form, FormStateInterface $form_state) {
// Add a placeholder as example that one can choose an arbitrary site name.
$form['site_information']['site_name']['#attributes']['placeholder'] = t('My Official Site Name');

Rajab Natshah
committed
// Default site email noreply@vardot.com .
$form['site_information']['site_mail']['#default_value'] = 'noreply@vardot.com';

Rajab Natshah
committed
$form['site_information']['site_mail']['#attributes']['style'] = 'width: 25em;';

Rajab Natshah
committed
// Default user 1 username should be 'webmaster'.
$form['admin_account']['account']['name']['#default_value'] = 'webmaster';
$form['admin_account']['account']['name']['#attributes']['disabled'] = TRUE;
$form['admin_account']['account']['mail']['#default_value'] = 'webmaster@vardot.com';
$form['admin_account']['account']['mail']['#description'] = t('In most case, and for <a target="_blank" href="@link">Vardot</a> specific use, we recommend this to always be <em>webmaster@vardot.com</em>.', array('@link' => 'http://vardot.com'));

Rajab Natshah
committed
}
/**
* Implements hook_install_tasks().
*/

Rajab Natshah
committed
function varbase_install_tasks(&$install_state) {
// Determine whether the enable multiligual option is selected during the
// Multilingual configuration task.
$needs_configure_multilingual = (isset($install_state['varbase']['enable_multilingual']) && $install_state['varbase']['enable_multilingual'] == TRUE);

Rajab Natshah
committed
return array(

Rajab Natshah
committed
'varbase_multilingual_configuration_form' => array(

Rajab Natshah
committed
'display_name' => t('Multilingual configuration'),
'display' => TRUE,
'type' => 'form',
'function' => ConfigureMultilingualForm::class,
),
'varbase_configure_multilingual' => array(
'display_name' => t('Configure multilingual'),
'display' => $needs_configure_multilingual,
'type' => 'batch',
),

Rajab Natshah
committed
'varbase_extra_components' => array(
'display_name' => t('Extra components'),
'display' => TRUE,
'type' => 'form',
'function' => AssemblerForm::class,
),
'varbase_assemble_extra_components' => array(
'display_name' => t('Assemble extra components'),
'display' => TRUE,
'type' => 'batch',
),

Rajab Natshah
committed
'varbase_development_tools' => array(
'display_name' => t('Development tools'),
'display' => TRUE,
'type' => 'form',
'function' => DevelopmentToolsAssemblerForm::class,
),
'varbase_assemble_development_tools' => array(
'display_name' => t('Assemble development tools'),
'display' => TRUE,
'type' => 'batch',
),

Rajab Natshah
committed
);
}

Rajab Natshah
committed
/**
* Implements hook_install_tasks_alter().
*/
function varbase_install_tasks_alter(array &$tasks, array $install_state) {
$tasks['install_finished']['function'] = 'varbase_after_install_finished';
}

Rajab Natshah
committed
/**
* Batch job to assemble Varbase extra components.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function varbase_assemble_extra_components(array &$install_state) {

Rajab Natshah
committed

Rajab Natshah
committed
// Default Varbase components, which must be installed.

Rajab Natshah
committed
$default_components = ConfigBit::getList('configbit/default.components.varbase.bit.yml', 'install_default_components', TRUE, 'dependencies', 'profile', 'varbase');

Rajab Natshah
committed

Rajab Natshah
committed
$batch = [];

Rajab Natshah
committed
// Install default components first.
foreach ($default_components as $default_component) {
$batch['operations'][] = ['varbase_assemble_extra_component_then_install', (array) $default_component];
}

Rajab Natshah
committed
// Install selected extra features.
$selected_extra_features = [];
$selected_extra_features_configs = [];

Rajab Natshah
committed

Rajab Natshah
committed
if (isset($install_state['varbase']['extra_features_values'])) {
$selected_extra_features = $install_state['varbase']['extra_features_values'];
}
if (isset($install_state['varbase']['extra_features_configs'])) {
$selected_extra_features_configs = $install_state['varbase']['extra_features_configs'];
}
// Get the list of extra features config bits.
$extraFeatures = ConfigBit::getList('configbit/extra.components.varbase.bit.yml', 'show_extra_components', TRUE, 'dependencies', 'profile', 'varbase');
// If we do have selected extra features.
if (count($selected_extra_features) && count($extraFeatures)) {

Rajab Natshah
committed
// Have batch processes for each selected extra features.

Rajab Natshah
committed
foreach ($selected_extra_features as $extra_feature_key => $extra_feature_checked) {
if ($extra_feature_checked) {

Rajab Natshah
committed
// If the extra feature was a module and not enabled, then enable it.
if (!\Drupal::moduleHandler()->moduleExists($extra_feature_key)) {
// Add the checked extra feature to the batch process to be enabled.
$batch['operations'][] = ['varbase_assemble_extra_component_then_install', (array) $extra_feature_key];
}

Rajab Natshah
committed

Rajab Natshah
committed
if (count($selected_extra_features_configs) &&

Rajab Natshah
committed
isset($extraFeatures[$extra_feature_key]['config_form']) &&
$extraFeatures[$extra_feature_key]['config_form'] == TRUE &&

Rajab Natshah
committed
isset($extraFeatures[$extra_feature_key]['formbit'])) {

Rajab Natshah
committed

Rajab Natshah
committed
$formbit_file_name = drupal_get_path('profile', 'varbase') . '/' . $extraFeatures[$extra_feature_key]['formbit'];

Rajab Natshah
committed

Rajab Natshah
committed
if (file_exists($formbit_file_name)) {

Rajab Natshah
committed
// Added the selected extra feature configs to the batch process
// with the same function name in the formbit.

Rajab Natshah
committed
$batch['operations'][] = ['varbase_save_editable_config_values',
(array) [
$extra_feature_key,
$formbit_file_name,
$selected_extra_features_configs,
],
];

Rajab Natshah
committed
}
}
}
}

Rajab Natshah
committed
// Hide Wornings and status messages.
$batch['operations'][] = ['varbase_hide_warning_and_status_messages', (array) TRUE];
// Fix entity updates to clear up any mismatched entity.
$batch['operations'][] = ['varbase_fix_entity_update', (array) TRUE];

Rajab Natshah
committed
}
// Install selected Demo content.
$selected_demo_content = [];
$selected_demo_content_configs = [];

Rajab Natshah
committed

Rajab Natshah
committed
if (isset($install_state['varbase']['demo_content_values'])) {
$selected_demo_content = $install_state['varbase']['demo_content_values'];
}
if (isset($install_state['varbase']['demo_content_configs'])) {
$selected_demo_content_configs = $install_state['varbase']['demo_content_configs'];
}

Rajab Natshah
committed
// Get the list of demo content config bits.

Rajab Natshah
committed
$demoContent = ConfigBit::getList('configbit/demo.content.varbase.bit.yml', 'show_demo', TRUE, 'dependencies', 'profile', 'varbase');

Rajab Natshah
committed
// If we do have demo_content and we have selected demo_content.
if (count($selected_demo_content) && count($demoContent)) {
// Have batch processes for each selected demo content.

Rajab Natshah
committed
foreach ($selected_demo_content as $demo_content_key => $demo_content_checked) {
if ($demo_content_checked) {

Rajab Natshah
committed
// If the demo content was a module and not enabled, then enable it.
if (!\Drupal::moduleHandler()->moduleExists($demo_content_key)) {
// Add the checked demo content to the batch process to be enabled.
$batch['operations'][] = ['varbase_assemble_extra_component_then_install', (array) $demo_content_key];
}

Rajab Natshah
committed
if (count($selected_demo_content_configs) &&

Rajab Natshah
committed
isset($demoContent[$demo_content_key]['config_form']) &&
$demoContent[$demo_content_key]['config_form'] == TRUE &&

Rajab Natshah
committed
isset($demoContent[$demo_content_key]['formbit'])) {
$formbit_file_name = drupal_get_path('profile', 'varbase') . '/' . $demoContent[$demo_content_key]['formbit'];

Rajab Natshah
committed
if (file_exists($formbit_file_name)) {

Rajab Natshah
committed

Rajab Natshah
committed
// Added the selected development configs to the batch process

Rajab Natshah
committed
// with the same function name in the formbit.

Rajab Natshah
committed
$batch['operations'][] = ['varbase_save_editable_config_values',
(array) [
$demo_content_key,
$formbit_file_name,
$selected_demo_content_configs,
],
];

Rajab Natshah
committed
}
}
}
}

Rajab Natshah
committed
// Hide Wornings and status messages.
$batch['operations'][] = ['varbase_hide_warning_and_status_messages', (array) TRUE];

Rajab Natshah
committed

Rajab Natshah
committed
// Fix entity updates to clear up any mismatched entity.
$batch['operations'][] = ['varbase_fix_entity_update', (array) TRUE];
}

Rajab Natshah
committed

Rajab Natshah
committed
// Uninstall list of not needed modules after the config had been loaded.
// To be loaded from a ConfigBit yml file.
$uninstall_components = ['varbase_default_content'];
if (count($uninstall_components) > 0) {
foreach ($uninstall_components as $uninstall_component)
$batch['operations'][] = ['varbase_uninstall_component', (array) $uninstall_component];
}

Rajab Natshah
committed

Rajab Natshah
committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
return $batch;
}
/**
* Batch job to assemble Varbase extra components.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function varbase_assemble_development_tools(array &$install_state) {
$batch = [];
// Install selected Development tools.
$selected_development_tools = [];
$selected_development_configs = [];
if (isset($install_state['varbase']['development_tools_values'])) {
$selected_development_tools = $install_state['varbase']['development_tools_values'];
}
if (isset($install_state['varbase']['development_tools_configs'])) {
$selected_development_configs = $install_state['varbase']['development_tools_configs'];
}

Rajab Natshah
committed
// Development tools.

Rajab Natshah
committed
$developmentTools = ConfigBit::getList('configbit/development.tools.varbase.bit.yml', 'show_development_tools', TRUE, 'dependencies', 'profile', 'varbase');
// If we do have development tools and we have selected development tools.
if (count($selected_development_tools) && count($developmentTools)) {
// Have batch processes for each selected development tool.
foreach ($selected_development_tools as $development_tool_key => $development_tool_checked) {
if ($development_tool_checked) {

Rajab Natshah
committed
// If the development tool was a module and not enabled, then enable it.
if (!\Drupal::moduleHandler()->moduleExists($development_tool_key)) {

Rajab Natshah
committed
// Add checked development tool to the batch process to be enabled.

Rajab Natshah
committed
$batch['operations'][] = ['varbase_assemble_extra_component_then_install', (array) $development_tool_key];
}

Rajab Natshah
committed
if (count($selected_development_configs) &&
isset($developmentTools[$development_tool_key]['config_form']) &&
$developmentTools[$development_tool_key]['config_form'] == TRUE &&
isset($developmentTools[$development_tool_key]['formbit'])) {

Rajab Natshah
committed

Rajab Natshah
committed
$formbit_file_name = drupal_get_path('profile', 'varbase') . '/' . $developmentTools[$development_tool_key]['formbit'];

Rajab Natshah
committed
if (file_exists($formbit_file_name)) {

Rajab Natshah
committed
// Added the selected development configs to the batch process
// with the same function name in the formbit.

Rajab Natshah
committed
$batch['operations'][] = ['varbase_save_editable_config_values',
(array) [
$development_tool_key,
$formbit_file_name,
$selected_development_configs,
],
];

Rajab Natshah
committed
}
}
}
}

Rajab Natshah
committed

Rajab Natshah
committed
// Hide Wornings and status messages.
$batch['operations'][] = ['varbase_hide_warning_and_status_messages', (array) TRUE];

Rajab Natshah
committed

Rajab Natshah
committed
// Fix entity updates to clear up any mismatched entity.
$batch['operations'][] = ['varbase_fix_entity_update', (array) TRUE];
}

Rajab Natshah
committed
return $batch;
}
/**
* Batch job to configure multilingual components.
*
* @param array $install_state
* The current install state.
*
* @return array
* The batch job definition.
*/
function varbase_configure_multilingual(array &$install_state) {
$batch = array();
// If the multiligual config checkbox were checked.
if (isset($install_state['varbase']['enable_multilingual'])
&& $install_state['varbase']['enable_multilingual'] == TRUE) {
// Install the Varbase internationalization feature module.
$batch['operations'][] = ['varbase_assemble_extra_component_then_install', (array) 'varbase_internationalization'];
// Add all selected languages and then translatvarbase_hide_messagesion
// will fetched for theme.

Rajab Natshah
committed
if (isset($install_state['varbase']['multilingual_languages'])
&& is_array($install_state['varbase']['multilingual_languages'])) {
foreach ($install_state['varbase']['multilingual_languages'] as $language_code) {
$batch['operations'][] = ['varbase_configure_language_and_fetch_traslation', (array) $language_code];
}

Rajab Natshah
committed
}
// Hide Wornings and status messages.
$batch['operations'][] = ['varbase_hide_warning_and_status_messages', (array) TRUE];
// Change configurations to work with enable_multilingual.
$batch['operations'][] = ['varbase_config_bit_for_multilingual', (array) TRUE];
}
else {
// Change configurations to work with NO multilingual.
$batch['operations'][] = ['varbase_config_bit_for_multilingual', (array) FALSE];
}
// Fix entity updates to clear up any mismatched entity.
$batch['operations'][] = ['varbase_fix_entity_update', (array) TRUE];

Rajab Natshah
committed
return $batch;
}
/**

Rajab Natshah
committed
* Batch function to assemble and install needed extra components.

Rajab Natshah
committed
*
* @param string|array $extra_component

Rajab Natshah
committed
* Name of the extra component.

Rajab Natshah
committed
*/
function varbase_assemble_extra_component_then_install($extra_component) {

Rajab Natshah
committed
\Drupal::service('module_installer')->install((array) $extra_component, TRUE);

Rajab Natshah
committed
}

Rajab Natshah
committed

Rajab Natshah
committed
/**
* Batch function to save editable config values for extra components.
*
* @param string|array $extra_component_machine_name

Rajab Natshah
committed
* Machine name key of the extra component.

Rajab Natshah
committed
* @param string|array $formbit_file_name
* FormBit file name.
* @param string|array $editable_config_values
* Editable config values.
*/
function varbase_save_editable_config_values($extra_component_machine_name, $formbit_file_name, $editable_config_values) {
include_once $formbit_file_name;
call_user_func_array($extra_component_machine_name . "_submit_formbit", array($editable_config_values));
}

Rajab Natshah
committed
/**
* Batch function to add selected langauges then fetch all traslation.
*
* @param string|array $language_code
* Language code to install and fetch all traslation.
*/
function varbase_configure_language_and_fetch_traslation($language_code) {
ConfigurableLanguage::createFromLangcode($language_code)->save();
}
/**
* Batch function to fix entity updates to clear up any mismatched entity.
*
* Entity and/or field definitions, The following changes were detected in
* the entity type and field definitions.
*
* @param string|array $entity_update
* To entity update or not.
*/
function varbase_fix_entity_update($entity_update) {
if ($entity_update) {

Rajab Natshah
committed
\Drupal::classResolver()
->getInstanceFromDefinition(VarbaseEntityDefinitionUpdateManager::class)
->applyUpdates();

Rajab Natshah
committed
}
}
/**
* Batch function to update configs with config bit configurations.
*
* @param bool $enable_multilingual
* Use multilignual in the site.
*/
function varbase_config_bit_for_multilingual($enable_multilingual) {

Rajab Natshah
committed

Rajab Natshah
committed
// Change configurations to work with enable_multilingual.

Rajab Natshah
committed
// Each module will manage its multilingual config.

Rajab Natshah
committed
}

Rajab Natshah
committed
/**
* Batch function to Uninstall list of not needed modules after the config had been loaded.
*
* @param string|array $uninstall_component
* Name of the extra component.
*/
function varbase_uninstall_component($uninstall_component) {
if (\Drupal::moduleHandler()->moduleExists($uninstall_component)) {
\Drupal::service('module_installer')->uninstall((array) $uninstall_component, FALSE);
}
}

Rajab Natshah
committed
/**
* Varbase after install finished.
*
* Lanuch auto Varbase Tour auto launch after install.
*
* @param array $install_state
* The current install state.
*
* @return array
* A renderable array with a redirect header.
*/
function varbase_after_install_finished(array &$install_state) {

Rajab Natshah
committed

Rajab Natshah
committed
// Mark all updates by the update helper checklist as successful on install.
if (\Drupal::moduleHandler()->moduleExists('update_helper_checklist')) {
$checkList = \Drupal::service('update_helper_checklist.update_checklist');
$checkList->markAllUpdates();
}
// Activate Varbase Bootstrap Paragraphs Settings in the active config.
if (\Drupal::moduleHandler()->moduleExists('varbase_bootstrap_paragraphs')) {
$profile_path = drupal_get_path('profile', 'varbase') . '/config/optional/';
$config_path = $profile_path . 'varbase_bootstrap_paragraphs.settings.yml';
$config_content = file_get_contents($config_path);
$config_data = (array) Yaml::parse($config_content);
$config_factory = \Drupal::configFactory()->getEditable('varbase_bootstrap_paragraphs.settings');
$config_factory->setData($config_data)->save(TRUE);
}

Rajab Natshah
committed

Rajab Natshah
committed
global $base_url;

Mohammed Razem
committed

Rajab Natshah
committed
// After install direction.
$after_install_direction = $base_url . '/?welcome';

Mohammed Razem
committed

Rajab Natshah
committed
install_finished($install_state);
$output = [];
// Clear all messages.
drupal_get_messages();
$output = [
'#title' => t('Varbase'),
'info' => [

Mohammed Razem
committed
'#markup' => t('<p>Congratulations, you have installed Varbase!</p><p>If you are not redirected to the front page in 5 seconds, Please <a href="@url">click here</a> to proceed to your installed site.</p>', [

Rajab Natshah
committed
'@url' => $after_install_direction,
]),
],
'#attached' => [
'http_header' => [
['Cache-Control', 'no-cache'],
],
],
];
$meta_redirect = [
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'refresh',
'content' => '0;url=' . $after_install_direction,
],
];
$output['#attached']['html_head'][] = [$meta_redirect, 'meta_redirect'];
return $output;
}

Rajab Natshah
committed
/**
* Batch function to hide warning messages.
*
* @param bool $hide
* To hide or not.
*/
function varbase_hide_warning_and_status_messages($hide) {
if ($hide && !isset($_SESSION['messages']['error'])) {
unset($_SESSION['messages']);
}
}
/**
* Implements hook_toolbar_alter().
*/
function varbase_toolbar_alter(&$items) {
if (\Drupal::currentUser()->hasPermission('access toolbar')
&& !empty($items['admin_toolbar_tools'])) {
$items['admin_toolbar_tools']['#attached']['library'][] = 'varbase/toolbar.icon';
}
}