You are here

protected function SocialGroupSelectorWidget::getOptions in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  2. 8.2 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  3. 8.3 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  4. 8.4 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  5. 8.5 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  6. 8.6 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  7. 8.7 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  8. 8.8 modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  9. 10.3.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  10. 10.0.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  11. 10.1.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()
  12. 10.2.x modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php \Drupal\social_group\Plugin\Field\FieldWidget\SocialGroupSelectorWidget::getOptions()

Returns the array of options for the widget.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which to return options.

Return value

array The array of options for the widget.

Overrides OptionsWidgetBase::getOptions

File

modules/social_features/social_group/src/Plugin/Field/FieldWidget/SocialGroupSelectorWidget.php, line 90

Class

SocialGroupSelectorWidget
A widget to select a group when creating an entity in a group.

Namespace

Drupal\social_group\Plugin\Field\FieldWidget

Code

protected function getOptions(FieldableEntityInterface $entity) {
  if (!isset($this->options)) {

    // Must be a node.
    if ($entity
      ->getEntityTypeId() !== 'node') {

      // We only handle nodes. When using this widget on other content types,
      // we simply return the normal options.
      return parent::getOptions($entity);
    }

    // Get the bundle fron the node.
    $entity_type = $entity
      ->bundle();

    /** @var \Drupal\user\Entity\User $account */
    $account = $this->userManager
      ->load($this->currentUser
      ->id());

    // If the user can administer content and groups, we allow them to
    // override this. Otherwise we stick to the original owner.
    if (!$account
      ->hasPermission('administer nodes') && !$account
      ->hasPermission('manage all groups')) {
      $account = $entity
        ->getOwner();
    }

    // Limit the settable options for the current user account.
    $options = $this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getOptionsProvider($this->column, $entity)
      ->getSettableOptions($account);

    // Check for each group type if the content type is installed.
    foreach ($options as $key => $optgroup) {

      // Groups are in the array below.
      if (is_array($optgroup)) {

        // Loop through the groups.
        foreach ($optgroup as $gid => $title) {

          // If the group exists.
          if ($group = Group::load($gid)) {

            // Load all installed plugins for this group type.
            $plugin_ids = $this->pluginManager
              ->getInstalledIds($group
              ->getGroupType());

            // If the bundle is not installed,
            // then unset the entire optiongroup (=group type).
            if (!in_array('group_node:' . $entity_type, $plugin_ids)) {
              unset($options[$key]);
            }
          }

          // We need to check only one of each group type,
          // so break out the second each.
          break;
        }
      }
    }

    // Remove groups the user does not have create access to.
    if (!$account
      ->hasPermission('manage all groups')) {
      $options = $this
        ->removeGroupsWithoutCreateAccess($options, $account, $entity);
    }

    // Add an empty option if the widget needs one.
    if ($empty_label = $this
      ->getEmptyLabel()) {
      $options = [
        '_none' => $empty_label,
      ] + $options;
    }
    $module_handler = $this->moduleHander;
    $context = [
      'fieldDefinition' => $this->fieldDefinition,
      'entity' => $entity,
    ];
    $module_handler
      ->alter('options_list', $options, $context);
    array_walk_recursive($options, [
      $this,
      'sanitizeLabel',
    ]);
    $this->options = $options;
  }
  return $this->options;
}