You are here

modal_page.install in Modal 8.3

Install, update and uninstall functions for the Modal Page.

File

modal_page.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Modal Page.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Url;
use Drupal\modal_page\Helper\ModalPageFieldHelper;
use Drupal\modal_page\Entity\Modal;

/**
 * Created new field "Close Modal with ESC and Clicling Outside".
 */
function modal_page_update_40002() {
  $modalPageFieldHelper = new ModalPageFieldHelper();
  $closeModalEscKey = $modalPageFieldHelper
    ->getFieldCloseModalEscKey();
  $closeModalClickingOutside = $modalPageFieldHelper
    ->getFieldCloseClickingOutside();
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('close_modal_esc_key', 'modal_page_modal', 'modal_page_modal', $closeModalEscKey);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('close_modal_clicking_outside', 'modal_page_modal', 'modal_page_modal', $closeModalClickingOutside);
  $field_close_esc_key = [
    'close_modal_esc_key' => 1,
  ];
  modal_page_update_modal_table($field_close_esc_key);
  $field_close_clicking_outside = [
    'close_modal_clicking_outside' => 1,
  ];
  modal_page_update_modal_table($field_close_clicking_outside);
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Created new field to choose label "Don't show again".
 */
function modal_page_update_40001() {
  $modalPageFieldHelper = new ModalPageFieldHelper();
  $fieldDontShowAgainLabel = $modalPageFieldHelper
    ->getFieldDontShowAgainLabel();
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('dont_show_again_label', 'modal_page_modal', 'modal_page_modal', $fieldDontShowAgainLabel);
  $field_to_be_updated = [
    'dont_show_again_label' => t("Don't show again"),
  ];
  modal_page_update_modal_table($field_to_be_updated);
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Created new field to restrict Modal by Role.
 */
function modal_page_update_8208() {
  $modalPageFieldHelper = new ModalPageFieldHelper();
  $fieldRole = $modalPageFieldHelper
    ->getFieldRole();
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('roles', 'modal_page_modal', 'modal_page_modal', $fieldRole);
}

/**
 * Created new field "Don't show again".
 */
function modal_page_update_8207() {
  $field_storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t("Enable <b>Don't show again</b> option"))
    ->setDefaultValue(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => -5,
  ]);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('enable_dont_show_again_option', 'modal_page_modal', 'modal_page_modal', $field_storage_definition);
  $field_to_be_updated = [
    'enable_dont_show_again_option' => TRUE,
  ];
  modal_page_update_modal_table($field_to_be_updated);
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Update on Modal Views to be more organized.
 */
function modal_page_update_8206() {
  update_modal_page_views();
}

/**
 * Create Auto Open Feature.
 */
function modal_page_update_8205() {
  $field_storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t("Auto Open"))
    ->setDefaultValue(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => -5,
  ]);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('auto_open', 'modal_page_modal', 'modal_page_modal', $field_storage_definition);
  $field_to_be_updated = [
    'auto_open' => TRUE,
  ];
  modal_page_update_modal_table($field_to_be_updated);
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Add new field on entity to open modal by element click.
 */
function modal_page_update_8204() {
  update_entity_field_element_click();
}

/**
 * Implements hook_update_N().
 *
 * Hook to update views settings.
 */
function modal_page_update_8203() {
  update_modal_page_views();
}

/**
 * Implements hook_update_N().
 *
 * Hook to update the entity modal_page_modal.
 */
function modal_page_update_8202() {
  create_storage_published();
  populate_published_field();
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Implements hook_update_N().
 */
function modal_page_update_8201() {
  update_modal_page_views();
}

/**
 * Modal Page with Entities.
 */
function modal_page_update_8001() {
  \Drupal::entityDefinitionUpdateManager()
    ->applyUpdates();
  \Drupal::service('modal_page.modals')
    ->importModalConfigToEntity();
  $messenger = \Drupal::messenger();
  $messenger
    ->addStatus(t('Thanks for use Modal Page 2'));
  $messenger
    ->addStatus(t('You may create Modal by visiting: You may create Modal at: Administration » Structure » Modal'));
}

/**
 * Create the field published.
 */
function create_storage_published() {
  $field_storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t("Published"))
    ->setDefaultValue(TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => -5,
  ]);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('published', 'modal_page_modal', 'modal_page_modal', $field_storage_definition);
}

/**
 * Set field published.
 */
function populate_published_field() {
  $modals = \Drupal::entityTypeManager()
    ->getStorage('modal_page_modal')
    ->loadMultiple();
  foreach ($modals as $modal) {
    $modal->published
      ->setValue(TRUE);
    if ($modal->type->value == 'page' && empty(trim($modal->pages->value))) {
      $modal->published
        ->setValue(FALSE);
    }
    if ($modal->type->value == 'parameter' && empty(trim($modal->paramenters->value))) {
      $modal->published
        ->setValue(FALSE);
    }
    $modal
      ->save();
  }
}

/**
 * Update views settings.
 */
function update_modal_page_views() {
  if (empty(\Drupal::moduleHandler()
    ->moduleExists('views'))) {
    return FALSE;
  }
  $config_views_path = drupal_get_path('module', 'modal_page') . '/config/optional/views.view.list_modal.yml';
  $data = Yaml::decode(file_get_contents($config_views_path));
  \Drupal::configFactory()
    ->getEditable('views.view.list_modal')
    ->setData($data)
    ->save(TRUE);
  \Drupal::service('cache.render')
    ->invalidateAll();
}

/**
 * Create field to open modal by element click.
 */
function update_entity_field_element_click() {
  $field_storage_definition = BaseFieldDefinition::create('string')
    ->setLabel(t('Open this modal clicking on this element'))
    ->setDescription(t('Example: <b>.open-modal-welcome</b>'))
    ->setRequired(FALSE)
    ->setSettings([
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDefaultValue(NULL)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('open_modal_on_element_click', 'modal_page_modal', 'modal_page_modal', $field_storage_definition);
}

/**
 * Update Modal Page Table.
 */
function modal_page_update_modal_table(array $fields) {
  if (empty(\Drupal::service('database')
    ->schema()
    ->tableExists('modal'))) {
    return FALSE;
  }
  $database = \Drupal::service('database');
  $database
    ->update('modal')
    ->fields($fields)
    ->execute();
  return TRUE;
}

/**
 * Implements hook_install().
 */
function modal_page_install() {
  $title = 'Thank you for installing Modal Page';
  $body = '<p>You can create your Modal at <a href="/admin/structure/modal/add">Structure » Modal » Add Modal</a></p>';
  $body .= '<p>Have fun :)</p>';
  $body .= '<p>If liked, give us a <a href="https://www.drupal.org/project/modal_page" target="blank">star</a></p>';
  $type = 'page';
  $pages = '/admin/*';
  $modal = Modal::create([
    'title' => $title,
    'body' => $body,
    'type' => $type,
    'pages' => $pages,
    'roles' => [
      'administrator',
      'authenticated',
    ],
  ]);
  $modal
    ->save();
  \Drupal::service('cache.render')
    ->invalidateAll();
  $messenger = \Drupal::messenger();
  $messenger
    ->addStatus(t('Modal Page load bootstrap.min.js by default, if this library is already loaded in other place, you can check a option <b>Don\'t load external JS Bootstrap</b> on <a href="@modal_page_settings">Modal Settings</a>', [
    '@modal_page_settings' => Url::fromRoute('modal_page.settings')
      ->toString(),
  ]));
}

/**
 * Implements hook_uninstall().
 */
function modal_page_uninstall() {
  \Drupal::configFactory()
    ->getEditable('modal_page.settings')
    ->delete();
  \Drupal::configFactory()
    ->getEditable('views.view.list_modal')
    ->delete();
}

Functions

Namesort descending Description
create_storage_published Create the field published.
modal_page_install Implements hook_install().
modal_page_uninstall Implements hook_uninstall().
modal_page_update_40001 Created new field to choose label "Don't show again".
modal_page_update_40002 Created new field "Close Modal with ESC and Clicling Outside".
modal_page_update_8001 Modal Page with Entities.
modal_page_update_8201 Implements hook_update_N().
modal_page_update_8202 Implements hook_update_N().
modal_page_update_8203 Implements hook_update_N().
modal_page_update_8204 Add new field on entity to open modal by element click.
modal_page_update_8205 Create Auto Open Feature.
modal_page_update_8206 Update on Modal Views to be more organized.
modal_page_update_8207 Created new field "Don't show again".
modal_page_update_8208 Created new field to restrict Modal by Role.
modal_page_update_modal_table Update Modal Page Table.
populate_published_field Set field published.
update_entity_field_element_click Create field to open modal by element click.
update_modal_page_views Update views settings.