Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / CustomFormPermissions.php
Size: Mime:
<?php

namespace Drupal\custom_forms;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\custom_forms\Entity\CustomFormType;

/**
 * Provides dynamic permissions for custom forms of different types.
 *
 * @package Drupal\custom_forms
 */
class CustomFormPermissions {
  use StringTranslationTrait;

  /**
   * Return an array of custom form type permissions.
   *
   * @return array
   *   The custom form type permissions
   *   @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  public function customFormTypePermissions() {
    $permissions = [];

    // Generate the custom form permissions for all types.
    foreach (CustomFormType::loadMultiple() as $type) {
      $permissions += $this->buildPermissions($type);
    }

    return $permissions;
  }

  /**
   * Build the permissions for a custom form type.
   *
   * @param \Drupal\custom_forms\Entity\CustomFormType $type
   *   The custom form type to build the permissions for.
   *
   * @return array
   *   Returns an array of all the permissions.
   */
  protected function buildPermissions(CustomFormType $type) {
    $type_id = $type->id();
    $type_params = ['%type_name' => $type->label()];

    return [
      "create $type_id custom form" => [
        'title' => $this->t('%type_name: Create new custom form', $type_params),
      ],
      "edit own $type_id custom form" => [
        'title' => $this->t('%type_name: Edit own custom form', $type_params),
      ],
      "edit any $type_id custom form" =>  [
        'title' => $this->t('%type_name: Edit any custom form', $type_params),
      ],
      "delete own $type_id custom form" => [
        'title' => $this->t('%type_name: Delete own custom form', $type_params),
      ],
      "delete any $type_id custom form" => [
        'title' => $this->t('%type_name: Delete any custom form', $type_params),
      ],
      "view $type_id revisions" => [
        'title' => $this->t('%type_name: View revisions', $type_params),
        'description' => $this->t('To view a revision, you also need permission to view the custom form'),
      ],
      "revert $type_id revisions" => [
        'title' => $this->t('%type_name: Revert revisions', $type_params),
        'description' => $this->t('To revert a revision, you also need permission to edit the custom form'),
      ],
      "delete $type_id revisions" => [
        'title' => $this->t('%type_name: Delete revisions', $type_params),
        'description' => $this->t('To delete a revision, you also need permission to delete the custom form'),
      ],
    ];
  }
}