You are here

function modal_page_update_40003 in Modal 5.0.x

Same name and namespace in other branches
  1. 4.0.x modal_page.install \modal_page_update_40003()
  2. 4.1.x modal_page.install \modal_page_update_40003()

Create Modal Page Entity.

File

./modal_page.install, line 22
Install, update and uninstall functions for the Modal Page.

Code

function modal_page_update_40003() {
  $modalEntity = new ConfigEntityType([
    'id' => 'modal',
    'label' => new TranslatableMarkup('Modal'),
    'config_prefix' => 'modal',
    'admin_permission' => 'administer modal page',
    'label_callback' => 'getLabelFromPlugin',
    'entity_keys' => [
      'id' => 'id',
      'label' => 'label',
      'uuid' => 'uuid',
      'body' => 'body',
      'pages' => 'pages',
      'parameters' => 'parameters',
      'auto_open' => 'auto_open',
      'open_modal_on_element_click' => 'open_modal_on_element_click',
      'langcode' => 'langcode',
      'ok_label_button' => 'ok_label_button',
      'enable_dont_show_again_option' => 'enable_dont_show_again_option',
      'dont_show_again_label' => 'dont_show_again_label',
      'modal_size' => 'modal_size',
      'close_modal_esc_key' => 'close_modal_esc_key',
      'close_modal_clicking_outside' => 'close_modal_clicking_outside',
      'roles' => 'roles',
      'type' => 'type',
      'delay_display' => 'delay_display',
      'published' => 'published',
    ],
    'config_export' => [
      'id',
      'label',
      'uuid',
      'body',
      'pages',
      'parameters',
      'auto_open',
      'open_modal_on_element_click',
      'langcode',
      'ok_label_button',
      'enable_dont_show_again_option',
      'dont_show_again_label',
      'modal_size',
      'close_modal_esc_key',
      'close_modal_clicking_outside',
      'roles',
      'type',
      'delay_display',
      'published',
    ],
  ]);
  \Drupal::entityDefinitionUpdateManager()
    ->installEntityType($modalEntity);

  // Delete old Views.
  \Drupal::service('config.factory')
    ->getEditable('views.view.' . 'list_modal')
    ->delete();

  // Clear Views' cache.
  drupal_flush_all_caches();

  // Clear cache.
  \Drupal::service('cache.render')
    ->invalidateAll();

  // Load Modals.
  $database = \Drupal::database();
  $query = $database
    ->select('modal', 'm')
    ->fields('m');
  $modals = $query
    ->execute();

  // Create a Modal Backup.
  $modals = $modals
    ->fetchAllAssoc('id');

  // Convert Object to Array.
  $modals = json_decode(json_encode($modals), TRUE);

  // Serialize Data to Backup.
  $modalBackup = serialize($modals);

  // Save backup.
  $config = \Drupal::service('config.factory')
    ->getEditable('modal_page.settings');
  $config
    ->set('modal_backup_content_entity', $modalBackup);
  $config
    ->save();

  // Insert Modals in Config Entity.
  foreach ($modals as $modalId => $modal) {
    $modalEntityId = 0;
    if (!empty($modal['id'])) {
      $modalEntityId = $modal['id'];
    }
    $title = '';
    if (!empty($modal['title'])) {
      $title = $modal['title'];
    }
    $body = '';
    if (!empty($modal['body__value'])) {
      $body = $modal['body__value'];
    }
    $type = 'page';
    if (!empty($modal['type'])) {
      $type = $modal['type'];
    }
    $pages = '';
    if (!empty($modal['pages'])) {
      $pages = $modal['pages'];
    }
    $parameters = '';
    if (!empty($modal['parameters'])) {
      $parameters = $modal['parameters'];
    }
    $autoOpen = 0;
    if (!empty($modal['auto_open'])) {
      $autoOpen = $modal['auto_open'];
    }
    $openModalOnElementClick = '';
    if (!empty($modal['open_modal_on_element_click'])) {
      $openModalOnElementClick = $modal['open_modal_on_element_click'];
    }
    $enableDontShowAgainOption = 1;
    if (empty($modal['enable_dont_show_again_option'])) {
      $enableDontShowAgainOption = $modal['enable_dont_show_again_option'];
    }
    $dontShowAgainLabel = t("Don't show again");
    if (!empty($modal['dont_show_again_label'])) {
      $dontShowAgainLabel = $modal['dont_show_again_label'];
    }
    $closeModalEscKey = 1;
    if (empty($modal['close_modal_esc_key'])) {
      $closeModalEscKey = $modal['close_modal_esc_key'];
    }
    $closeModalClickingOutside = 1;
    if (empty($modal['close_modal_clicking_outside'])) {
      $closeModalClickingOutside = $modal['close_modal_clicking_outside'];
    }
    $published = 0;
    if (!empty($modal['published'])) {
      $published = $modal['published'];
    }
    $delayDisplay = 0;
    if (!empty($modal['delay_display'])) {
      $delayDisplay = $modal['delay_display'];
    }
    $modalSize = 'modal-md';
    if (!empty($modal['modal_size'])) {
      $modalSize = $modal['modal_size'];
    }
    $okLabelButton = t('OK');
    if (!empty($modal['ok_label_button'])) {
      $okLabelButton = $modal['ok_label_button'];
    }
    $langcode = '';
    if (!empty($modal['langcode'])) {
      $langcode = $modal['langcode'];
    }

    // Get Roles.
    $roles = [];
    $databaseRoles = \Drupal::database();
    $queryRoles = $databaseRoles
      ->select('modal_page_modal__roles', 'mpmr')
      ->fields('mpmr');
    $queryRoles
      ->condition('mpmr.entity_id', $modalEntityId, '=');
    $modalPageRoles = $queryRoles
      ->execute()
      ->fetchAll();

    // Convert Object to Array.
    $modalPageRoles = json_decode(json_encode($modalPageRoles), TRUE);
    foreach ($modalPageRoles as $modalPageRole) {
      $modalPageRole = json_decode(json_encode($modalPageRole), TRUE);
      if (!empty($modalPageRole['roles_value'])) {
        $role = $modalPageRole['roles_value'];
        $roles[$role] = $role;
      }
    }
    $modal = \Drupal::entityTypeManager()
      ->getStorage('modal')
      ->create();
    $id = strtolower($title);
    $id = str_replace(' ', '_', $id);
    if ($id == 'thank_you_for_installing_modal_page') {
      continue;
    }
    $id = $id . '_' . $modalId;
    $modal
      ->setId($id);
    $modal
      ->setLabel($title);
    $modal
      ->setBody($body);
    $modal
      ->setType($type);
    $modal
      ->setPages($pages);
    $modal
      ->setParameters($parameters);
    $modal
      ->setRoles($roles);
    $modal
      ->setPublished($published);
    $modal
      ->setDelayDisplay($delayDisplay);
    $modal
      ->setAutoOpen($autoOpen);
    $modal
      ->setOpenModalOnElementClick($openModalOnElementClick);
    $modal
      ->setEnableDontShowAgainOption($enableDontShowAgainOption);
    $modal
      ->setModalSize($modalSize);
    $modal
      ->setOkLabelButton($okLabelButton);
    $modal
      ->setLangCode($langcode);
    $modal
      ->setDontShowAgainLabel($dontShowAgainLabel);
    $modal
      ->setCloseModalEscKey($closeModalEscKey);
    $modal
      ->setCloseModalClickingOutside($closeModalClickingOutside);
    $modal
      ->save();
  }
  $roles = Role::loadMultiple();
  foreach ($roles as $rid => $role) {

    // Skyp anonymous and authenticated.
    if ($rid == 'anonymous' || $rid == 'authenticated') {
      continue;
    }

    // If has permission, that's ok.
    if (!empty($role
      ->hasPermission('administer modal page'))) {
      continue;
    }

    // Check if have all permissions to receive the new one.
    if (!empty($role
      ->hasPermission('delete modal entity')) && !empty($role
      ->hasPermission('add modal entity')) && !empty($role
      ->hasPermission('view modal entity')) && !empty($role
      ->hasPermission('edit modal entity')) && !empty($role
      ->hasPermission('administer modal entity'))) {
      $role
        ->grantPermission('administer modal page');
      $role
        ->save();
    }
  }
  modal_page_add_modal_thanks_for_upgrade_4();
}