niobi-8.x-2.0-alpha4/modules/niobi_form/niobi_form.module

modules/niobi_form/niobi_form.module
<?php

/**
 * @file
 * Contains niobi_form.module.
 */

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\niobi_form\Entity\NiobiForm;
use Drupal\niobi_form\Entity\NiobiFormInterface;
use Drupal\webform\Element\WebformMessage;
use Drupal\webform\Entity\Webform;

/**
 * Implements hook_entity_type_alter().
 */
function niobi_form_entity_type_alter(array &$entity_types) {
  if (isset($entity_types['webform'])) {
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $webform_entity_type */
    $webform_entity_type = $entity_types['webform'];
    $webform_entity_type->setLinkTemplate('references', '/admin/structure/webform/manage/{webform}/references');
  }
}

/**
 * Implements hook_help().
 */
function niobi_form_help($route_name, RouteMatchInterface $route_match) {
  // Don't show the warning if the user can't create webforms.
  if (!\Drupal::currentUser()->hasPermission('create webform')) {
    return NULL;
  }

  switch ($route_name) {
    // Main module help for the niobi_form module.
    case 'help.page.niobi_form':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Creates a webform-enabled entity for groups and departments') . '</p>';
      return $output;
      break;

    case 'niobi_form.add':
      /** @var \Drupal\niobi_form\Entity\NiobiFormTypeInterface $niobi_form_type */
      $niobi_form_type = $route_match->getParameter('niobi_form_type');

      // Determine if the niobi_form type has webform (entity reference) field.
      $has_webform_field = FALSE;
      $field_configs = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['entity_type' => 'niobi_form', 'bundle' => $niobi_form_type->id()]);
      foreach ($field_configs as $field_config) {
        if ($field_config->get('field_type') === 'webform') {
          $has_webform_field = TRUE;
          break;
        }
      }

      // Display warning message if webform query string parameter is missing.
      if ($has_webform_field && !\Drupal::request()->get('webform_id')) {
        $build = [
          '#type' => 'webform_message',
          '#message_type' => 'warning',
          '#message_close' => TRUE,
          '#message_id' => 'niobi_form.references',
          '#message_storage' => WebformMessage::STORAGE_USER,
          '#message_message' => t('Webforms must first be <a href=":href">created</a> before referencing them in the below form.', [':href' => Url::fromRoute('entity.webform.collection')->toString()]),
          '#cache' => ['max-age' => 0],
        ];
      }
      elseif (\Drupal::request()->get('webform_id')) {
        // If there is a webform query string parameter, then disable caching.
        $build = ['#cache' => ['max-age' => 0]];
      }
      else {
        $build = [];
      }

      // Makes sure if a webform field is added to a niobi_form type it is accounted for.
      \Drupal::service('renderer')->addCacheableDependency($build, $niobi_form_type);
      return $build;
      break;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function niobi_form_theme() {
  $theme = [];
  $theme['niobi_form'] = [
    'render element' => 'elements',
    'file' => 'niobi_form.page.inc',
    'template' => 'niobi_form',
  ];
  $theme['niobi_form_content_add_list'] = [
    'render element' => 'content',
    'variables' => ['content' => NULL],
    'file' => 'niobi_form.page.inc',
  ];
  return $theme;
}

/**
* Implements hook_theme_suggestions_HOOK().
*/
function niobi_form_theme_suggestions_niobi_form(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#niobi_form'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'niobi_form__' . $sanitized_view_mode;
  $suggestions[] = 'niobi_form__' . $entity->bundle();
  $suggestions[] = 'niobi_form__' . $entity->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'niobi_form__' . $entity->id();
  $suggestions[] = 'niobi_form__' . $entity->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_niobi_form_access().
 */
function niobi_form_niobi_form_access(NiobiFormInterface $niobi_form, $operation, AccountInterface $account) {
  if (strpos($operation, 'webform_submission_') !== 0) {
    return AccessResult::neutral();
  }
  else {
    /** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
    $entity_reference_manager = \Drupal::service('webform.entity_reference_manager');

    // Check that the niobi_form has a webform field that has been populated.
    $webform = $entity_reference_manager->getWebform($niobi_form);
    if (!$webform) {
      return AccessResult::forbidden();
    }

    // Check administer webform submissions.
    if ($account->hasPermission('administer webform submission')) {
      return AccessResult::allowed();
    }

    // Change access to ANY submission.
    $operation = str_replace('webform_submission_', '', $operation);
    $any_permission = "$operation webform submissions any niobi_form";
    if ($account->hasPermission($any_permission)) {
      return AccessResult::allowed();
    }

    // Change access to submission associated with the niobi_form's webform.
    $own_permission = "$operation webform submissions own niobi_form";
    if ($account->hasPermission($own_permission) && $niobi_form->getOwnerId() === $account->id()) {
      return AccessResult::allowed();
    }

    return AccessResult::forbidden();
  }
}

/**
 * Implements hook_niobi_form_prepare_form().
 *
 * Prepopulate a niobi_form's webform field target id.
 *
 * @see \Drupal\niobi_form\Controller\NiobiFormReferencesListController::render
 */
function niobi_form_niobi_form_prepare_form(NiobiFormInterface $niobi_form, $operation, FormStateInterface $form_state) {
  // Only prepopulate new niobi_forms.
  if (!$niobi_form->isNew()) {
    return;
  }

  /** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
  $entity_reference_manager = \Drupal::service('webform.entity_reference_manager');

  // Make the niobi_form has a webform (entity reference) field.
  $field_name = $entity_reference_manager->getFieldName($niobi_form);
  if (!$field_name) {
    return;
  }

  // Populate the niobi_form's title and webform field.
  $webform_id = \Drupal::request()->query->get('webform_id');
  if ($webform_id && ($webform = Webform::load($webform_id))) {
    $niobi_form->title->value = $webform->label();
    $niobi_form->$field_name->target_id = $webform_id;
  }
}

/**
 * Implements hook_niobi_form_delete().
 *
 * Remove user specified entity references.
 */
function niobi_form_niobi_form_delete(NiobiFormInterface $niobi_form) {
  /** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
  $entity_reference_manager = \Drupal::service('webform.entity_reference_manager');
  $entity_reference_manager->deleteUserWebformId($niobi_form);
}

/**
 * Implements hook_preprocess_HOOK() for page title templates.
 */
function niobi_form_preprocess_page_title(&$variables) {
  $niobi_form = \Drupal::routeMatch()->getParameter('niobi_form');
  if ($niobi_form && is_string($niobi_form)) {
    $niobi_form = NiobiForm::load($niobi_form);
  }

  if (!$niobi_form) {
    return;
  }

  /** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
  $entity_reference_manager = \Drupal::service('webform.entity_reference_manager');

  // Only allow user to change webform for specific routes.
  if (!$entity_reference_manager->isUserWebformRoute($niobi_form)) {
    return;
  }

  $webforms = $entity_reference_manager->getWebforms($niobi_form);
  if (count($webforms) > 1) {
    $route_options = ['query' => \Drupal::destination()->getAsArray()];

    $operations = [];

    // Add current webform first.
    $current_webform = $entity_reference_manager->getWebform($niobi_form);
    $operations[$current_webform->id()] = [
      'title' => $current_webform->label(),
      'url' => Url::fromRoute('entity.niobi_form.webform.entity_reference.set', ['niobi_form' => $niobi_form->id(), 'webform' => $current_webform->id()], $route_options),
    ];

    // Add remaining webforms.
    foreach ($webforms as $webform) {
      $operations[$webform->id()] = [
        'title' => $webform->label(),
        'url' => Url::fromRoute('entity.niobi_form.webform.entity_reference.set', ['niobi_form' => $niobi_form->id(), 'webform' => $webform->id()], $route_options),
      ];
    }

    $variables['title_prefix']['niobi_form'] = [
      '#type' => 'operations',
      '#links' => $operations,
      '#prefix' => '<div class="webform-dropbutton niobi-form-entity-references">',
      '#suffix' => '</div>',
      '#attached' => [
        'library' => [
          'niobi_form/niobi_form.entity_references',
        ],
      ],
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc