You are here

social_font.install in Open Social 8

File

modules/custom/social_font/social_font.install
View source
<?php

/**
 * @file
 * Contains social_font.install.
 */
use Drupal\social_font\Entity\Font;
use Drupal\user\Entity\Role;

/**
 * The social_font install file.
 */
function social_font_install() {
  $font = Font::create([
    'name' => 'Montserrat',
    'user_id' => 1,
    'created' => \Drupal::time()
      ->getRequestTime(),
    'field_fallback' => '0',
  ]);
  $font
    ->save();

  // Set some default permissions.
  _social_font_set_permissions();
}

/**
 * Function to set permissions.
 */
function _social_font_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_font_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Build the permissions for each role.
 *
 * @param string $role
 *   The role.
 *
 * @return array
 *   Returns an array containing permissions.
 */
function _social_font_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], []);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], []);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], [
    'add font entities',
    'delete font entities',
    'edit font entities',
    'access font overview',
    'view published font entities',
    'view unpublished font entities',
  ]);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

Functions

Namesort descending Description
social_font_install The social_font install file.
_social_font_get_permissions Build the permissions for each role.
_social_font_set_permissions Function to set permissions.