You are here

protected function OgComplex::otherGroupsWidget in Organic groups 8

Adding the other groups widget to the form.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The existing items to add to the form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array A renderable element with the "other groups".

1 call to OgComplex::otherGroupsWidget()
OgComplex::form in src/Plugin/Field/FieldWidget/OgComplex.php
Creates a form element for a field.

File

src/Plugin/Field/FieldWidget/OgComplex.php, line 200

Class

OgComplex
Plugin implementation of the 'entity_reference autocomplete' widget.

Namespace

Drupal\og\Plugin\Field\FieldWidget

Code

protected function otherGroupsWidget(FieldItemListInterface $items, FormStateInterface $form_state) {
  if ($this->fieldDefinition
    ->getTargetEntityTypeId() === 'user') {
    $description = $this
      ->t('As groups administrator, associate this user with groups you do <em>not</em> belong to.');
  }
  else {
    $description = $this
      ->t('As groups administrator, associate this content with groups you do <em>not</em> belong to.');
  }
  $field_wrapper = Html::getClass($this->fieldDefinition
    ->getName()) . '-add-another-group';
  $elements = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#title' => $this
      ->t('Other groups'),
    '#description' => $description,
    '#prefix' => '<div id="' . $field_wrapper . '">',
    '#suffix' => '</div>',
    '#cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    '#cardinality_multiple' => TRUE,
    '#theme' => 'field_multiple_value_form',
    '#field_name' => $this->fieldDefinition
      ->getName(),
    '#max_delta' => 1,
  ];
  $elements['add_more'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Add another item'),
    '#name' => 'add_another_group',
    '#ajax' => [
      'callback' => [
        $this,
        'addMoreAjax',
      ],
      'wrapper' => $field_wrapper,
      'effect' => 'fade',
    ],
  ];
  $delta = 0;
  $target_type = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getSetting('target_type');

  /** @var \Drupal\og\MembershipManagerInterface $membership_manager */
  $membership_manager = \Drupal::service('og.membership_manager');
  $user_groups = $membership_manager
    ->getUserGroups(\Drupal::currentUser()
    ->id());
  $user_groups_target_type = isset($user_groups[$target_type]) ? $user_groups[$target_type] : [];
  $user_group_ids = array_map(function ($group) {
    return $group
      ->id();
  }, $user_groups_target_type);
  $other_groups_weight_delta = round(count($user_groups) / 2);
  foreach ($items
    ->referencedEntities() as $group) {
    if (in_array($group
      ->id(), $user_group_ids)) {
      continue;
    }
    $elements[$delta] = $this
      ->otherGroupsSingle($delta, $group, $other_groups_weight_delta);
    $delta++;
  }
  if (!$form_state
    ->get('other_group_delta')) {
    $form_state
      ->set('other_group_delta', $delta);
  }

  // Get the trigger element and check if this the add another item button.
  $trigger_element = $form_state
    ->getTriggeringElement();
  if (!empty($trigger_element) && $trigger_element['#name'] == 'add_another_group') {

    // Increase the number of other groups.
    $delta = $form_state
      ->get('other_group_delta') + 1;
    $form_state
      ->set('other_group_delta', $delta);
  }

  // Add another auto complete field.
  for ($i = $delta; $i <= $form_state
    ->get('other_group_delta'); $i++) {

    // Also add one to the weight delta, just to make sure.
    $elements[$i] = $this
      ->otherGroupsSingle($i, NULL, $other_groups_weight_delta + 1);
  }
  return $elements;
}