You are here

protected function CategorizedOptionsbuttonsWidget::getGroupedOptions in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  2. 8.3 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  3. 8.4 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  4. 8.5 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  5. 8.6 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  6. 8.7 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  7. 8.8 modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  8. 10.0.x modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  9. 10.1.x modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()
  10. 10.2.x modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php \Drupal\social_profile\Plugin\Field\FieldWidget\CategorizedOptionsbuttonsWidget::getGroupedOptions()

Group the options with their parent taxonomy terms.

This makes it easier to arrange them for display later. Relies on a proper ordering of parent > child from the taxonomy validation.

Parameters

array $options: The options to group.

array $selected: The currently selected options.

Return value

array A tree structure with labels and selection status as leaf values.

1 call to CategorizedOptionsbuttonsWidget::getGroupedOptions()
CategorizedOptionsbuttonsWidget::formElement in modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php
Returns the form for a single field widget.

File

modules/social_features/social_profile/src/Plugin/Field/FieldWidget/CategorizedOptionsbuttonsWidget.php, line 143

Class

CategorizedOptionsbuttonsWidget
Plugin implementation of the 'categorized_options_buttons' widget.

Namespace

Drupal\social_profile\Plugin\Field\FieldWidget

Code

protected function getGroupedOptions(array $options, array $selected) {
  $grouped_options = [];
  foreach ($options as $tid => $label) {

    // Start at the top for the current element.
    $group =& $grouped_options;

    // Find the position for this element based on its parent group. If it
    // contains a dash (-) then it's a child and we find its parent.
    while (strpos($label, '-') === 0) {

      // Remove the dash denoting this is a child.
      $label = substr($label, 1);

      // Move into the last parent added.
      end($group);
      $group =& $group[key($group)]['children'];
    }

    // This is now a new child to the currently selected parent.
    $group[$tid] = [
      'label' => $label,
      'selected' => in_array($tid, $selected),
    ];
  }
  return $grouped_options;
}