You are here

function webform_access_field_widget_form_alter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_access/webform_access.module \webform_access_field_widget_form_alter()

Implements hook_field_widget_form_alter().

File

modules/webform_access/webform_access.module, line 309
Provides webform access controls for webform nodes.

Code

function webform_access_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  /** @var \Drupal\Core\Field\FieldItemListInterface $items */
  $items = $context['items'];
  $field_definition = $items
    ->getFieldDefinition();
  if ($field_definition
    ->getType() !== 'webform') {
    return;
  }

  // Get the target entity.
  $entity = $items
    ->getEntity();

  // Only nodes are currently supported.
  if ($entity
    ->getEntityTypeId() !== 'node') {
    return;
  }
  $default_value = $entity
    ->id() ? \Drupal::database()
    ->select('webform_access_group_entity', 'ge')
    ->fields('ge', [
    'group_id',
  ])
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->condition('entity_id', $entity
    ->id())
    ->condition('webform_id', $element['target_id']['#default_value'])
    ->condition('field_name', $field_definition
    ->getName())
    ->execute()
    ->fetchCol() : ($items->webform_access_group ?: []);
  $element['settings']['webform_access_group'] = _webform_access_group_build_element($default_value, [], $form_state);
}