You are here

function gdpr_consent_field_widget_form in General Data Protection Regulation 7

Implements hook_field_widget_form().

File

modules/gdpr_consent/gdpr_consent.module, line 578
Contains hook implementations and shared functions.

Code

function gdpr_consent_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  global $user;
  $agreed = isset($items[$delta]['agreed']) ? (int) $items[$delta]['agreed'] : 0;
  $notes = isset($items[$delta]['notes']) ? $items[$delta]['notes'] : '';
  $widget = $element;
  $widget['#delta'] = $delta;

  // Get current revision of the referenced agreement entity.
  $entity_type = 'gdpr_consent_agreement';
  $entity = entity_load_single($entity_type, $field['settings']['target_id']);
  list(, $vid) = entity_extract_ids($entity_type, $entity);
  switch ($instance['widget']['type']) {
    case 'gdpr_consent_widget':
      $element['agreed'] = array(
        '#type' => 'checkbox',
        '#title' => $entity->description,
        '#default_value' => $agreed,
        '#weight' => 0,
        '#attributes' => array(
          'class' => array(
            'gdpr_consent_agreement',
          ),
        ),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'gdpr_consent') . '/css/gdpr_consent.css',
          ),
          'js' => array(
            drupal_get_path('module', 'gdpr_consent') . '/js/gdpr_consent.js',
          ),
        ),
        '#description' => $entity->long_description,
      ) + $widget;
      if ($entity->agreement_type != GDPR_CONSENT_TYPE_EXPLICIT) {
        $element['description'] = $element['agreed'];
        $element['description']['#type'] = 'markup';
        $element['description']['#markup'] = $element['description']['#title'];
        $element['agreed']['#type'] = 'value';
        $element['agreed']['#default_value'] = 1;
      }
      $element['notes'] = array(
        '#type' => 'textfield',
        '#title' => 'Consent Notes',
        '#default_value' => $notes,
        '#weight' => 10,
        '#description' => t('Consent notes only visible for staff.'),
        '#access' => user_access('grant any consent', $user),
      ) + $widget;
      $element['target_id'] = array(
        '#type' => 'hidden',
        '#value' => $field['settings']['target_id'],
      ) + $widget;
      $element['target_revision_id'] = array(
        '#type' => 'hidden',
        '#value' => $vid,
      ) + $widget;
      $element['date'] = array(
        '#type' => 'hidden',
        '#value' => REQUEST_TIME,
      ) + $widget;

      // @todo Build system for automatically finding consenting user.
      $element['user_consenter'] = array(
        '#type' => 'hidden',
        '#value' => $user->uid,
      ) + $widget;
      $element['user_recorder'] = array(
        '#type' => 'hidden',
        '#value' => $user->uid,
      ) + $widget;
      break;
  }
  return $element;
}