You are here

formassembly.module in FormAssembly 8

Same filename and directory in other branches
  1. 7 formassembly.module

Procedural code for formassembly module.

@author Shawn P. Duncan <code@sd.shawnduncan.org>

Copyright 2018 by Shawn P. Duncan. This code is released under the GNU General Public License. Which means that it is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.html

File

formassembly.module
View source
<?php

/**
 * @file
 * Procedural code for formassembly module.
 *
 * @author Shawn P. Duncan <code@sd.shawnduncan.org>
 *
 * Copyright 2018 by Shawn P. Duncan.  This code is
 * released under the GNU General Public License.
 * Which means that it is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * http://www.gnu.org/licenses/gpl.html
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Implements hook_help().
 */
function formassembly_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the formassembly module.
    case 'help.page.formassembly':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Integrates a Drupal site with the FormAssembly service') . '</p>';
      return $output;
    default:
      return [];
  }
}

/**
 * Implements hook_theme().
 */
function formassembly_theme($existing, $type, $theme, $path) {
  return [
    'fa_form__fa_form' => [
      'template' => 'fa_form__fa_form__markup',
      'base hook' => 'entity',
    ],
    'fa_form__external_js' => [
      'variables' => [
        'src' => NULL,
      ],
    ],
    'fa_form__inline_js' => [
      'variables' => [
        'value' => NULL,
      ],
    ],
    'fa_form__external_css' => [
      'variables' => [
        'value' => NULL,
        'rel' => NULL,
        'href' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_entity_field_access().
 */
function formassembly_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($operation == 'edit' && $field_definition
    ->getType() == 'entityreference' && $field_definition
    ->getSetting(target_type) == 'fa_form') {
    return AccessResult::allowedIfHasPermission($account, 'reference formassembly');
  }
  return AccessResult::neutral();
}

/**
 * Implementation of callback_batch_operation()
 */
function formassembly_batch_get_forms(&$context) {

  /** @var \Drupal\formassembly\FormAssemblyBatchProcessor $batchProcessor */
  $batchProcessor = \Drupal::service('formassembly.batch');
  if (empty($context['sandbox'])) {

    // Batch has not run before.  Initialize.
    $batchProcessor
      ->configureBatch($context);
  }
  $batchProcessor
    ->iterateBatch($context);
}

/**
 * Implementation of  callback_batch_finished()
 */
function formassembly_batch_finished($success, $results, $operations) {

  /** @var \Drupal\formassembly\FormAssemblyBatchProcessor $batchProcessor */
  $batchProcessor = \Drupal::service('formassembly.batch');
  if ($success) {
    $batchProcessor
      ->batchPostProcess($results['sync_id']);
  }
}