You are here

function gc_gc_process_choice_checkbox_field in GatherContent 8.3

Same name and namespace in other branches
  1. 8 gathercontent.module \gc_gc_process_choice_checkbox_field()

Processing function for checkbox type of field.

Parameters

\Drupal\node\NodeInterface $node: Object of node.

string $local_field_name: Local field name.

bool $is_translatable: Indicator if node is translatable.

string $language: Language of translation if applicable.

array $options: Array of options.

1 call to gc_gc_process_choice_checkbox_field()
gc_gc_process_content_pane in ./gathercontent.module
Processing function for content panes.

File

./gathercontent.module, line 590
Main module file for GatherContent module.

Code

function gc_gc_process_choice_checkbox_field(NodeInterface &$node, $local_field_name, $is_translatable, $language, array $options) {
  $field = FieldConfig::loadByName('node', $node
    ->bundle(), $local_field_name);
  $node->{$local_field_name} = [
    NULL,
  ];
  $selected_options = [];
  foreach ($options as $option) {
    if ($option->selected) {
      if ($field
        ->getType() === 'entity_reference') {

        /** @var \Drupal\taxonomy\Entity\Term $term */
        $term = array_shift(\Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadByProperties([
          'gathercontent_option_ids' => $option->name,
        ]));
        $selected_options[] = $term
          ->id();
      }
      else {
        $selected_options[] = $option->name;
      }
    }
    if ($is_translatable) {
      $node
        ->getTranslation($language)->{$local_field_name} = $selected_options;
    }
    else {
      $node->{$local_field_name} = $selected_options;
    }
  }
}