<?php

/**
 * @file
 * Contains opencase_defaults.module.
 */

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;



function opencase_defaults_client_callback():array  {
  return [\Drupal::request()->query->get('client_id')];
}
/**
 * Implements hook_help().
 */
function opencase_defaults_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the opencase_defaults module.
    case 'help.page.opencase_defaults':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('OpenCase Default Configuration') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function opencase_defaults_theme() {
  return [
    'opencase_defaults' => [
      'render element' => 'children',
    ],
  ];
}


function opencase_defaults_entity_base_field_info($entity_type) {
  $fields = array();
  
  // Add consent field to person
  if ($entity_type->id() === 'oc_actor') {
    $fields['consent'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Consent to data storage'))
      ->setDescription(t('Has this person explicitly consented to having their personal data stored on this system?'))
      ->setRevisionable(TRUE)
      ->setDefaultValue(FALSE)
      ->setRequired(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'boolean_checkbox',
        'weight' => -6,
      ));
  }
  if ($entity_type->id() === 'oc_case') {
    $fields['client'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel('Client')
      ->setSetting('target_type', 'oc_actor')
      ->setSetting('handler', 'default')
      ->setTranslatable(TRUE)
      ->setCardinality(1)
      ->setSetting('handler_settings', [
        ['target_bundles' => ['client' => 'client'] ]         
      ])
      ->setDisplayConfigurable('form', true)
      ->setDisplayConfigurable('view', true)
      ->setDefaultValueCallback('opencase_defaults_client_callback')
      ->setRequired(TRUE);
  }
  return $fields;
}