You are here

webform_invitation.install in Webform Invitation 8

Installation functions for the Webform Invitation module.

File

webform_invitation.install
View source
<?php

/**
 * @file
 * Installation functions for the Webform Invitation module.
 */

/**
 * Implements hook_schema().
 */
function webform_invitation_schema() {
  $schema['webform_invitation_codes'] = [
    'description' => 'Table for storing generated form tokens.',
    'fields' => [
      'cid' => [
        'description' => 'The primary identifier for a code.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'webform' => [
        'description' => 'The primary identifier for a webform.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ],
      'code' => [
        'description' => 'A code for the webform.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ],
      'created' => [
        'description' => 'The Unix timestamp when the code was generated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'used' => [
        'description' => 'The Unix timestamp when the code was used.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'sid' => [
        'description' => 'The submission ID using this code.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'cid',
    ],
    'unique keys' => [
      'webform_code' => [
        'webform',
        'code',
      ],
    ],
    'indexes' => [
      'webform' => [
        'webform',
      ],
      'used' => [
        'used',
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function webform_invitation_uninstall() {

  /** @var \Drupal\webform\WebformEntityStorageInterface $webform_storage */
  $webform_storage = \Drupal::service('entity_type.manager')
    ->getStorage('webform');

  // Remove Webform Invitation textfields from existing webforms with
  // invitation mode activated.
  $webform_ids = $webform_storage
    ->getQuery()
    ->condition('elements', 'webform_invitation_code', 'CONTAINS')
    ->execute();

  /** @var \Drupal\webform\Entity\Webform $webform */
  foreach ($webform_storage
    ->loadMultiple($webform_ids) as $webform) {
    $webform
      ->deleteElement('webform_invitation_code');
    $webform
      ->save();
  }
}

Functions