You are here

public static function BusinessRulesViewsSelection::updateDependentField in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php \Drupal\business_rules\Plugin\EntityReferenceSelection\BusinessRulesViewsSelection::updateDependentField()

Update the dependent field options.

Parameters

array $form: The form.

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

Return value

mixed The updated field.

File

src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php, line 161

Class

BusinessRulesViewsSelection
Plugin override of the 'selection' entity_reference.

Namespace

Drupal\business_rules\Plugin\EntityReferenceSelection

Code

public static function updateDependentField(array $form, FormStateInterface $form_state) {
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $trigger_field = $form_state
    ->getTriggeringElement();

  // Update children.
  $children = $trigger_field['#ajax']['br_children'];
  $response = new AjaxResponse();
  foreach ($children as $child) {
    $field_definition = $entity
      ->getFieldDefinitions();
    if ($field_definition[$child]
      ->getSetting('handler') == 'business_rules_views') {
      $handler_settings = $field_definition[$child]
        ->getSetting('handler_settings');
      $view = Views::getView($handler_settings['business_rules_view']['view_name']);
      $parent_field_value = $trigger_field['#value'];
      if ($trigger_field['#type'] === 'entity_autocomplete' && preg_match('/\\((\\d+)\\)$/', $parent_field_value, $matches)) {

        // If the field widget is entity autocomplete, the returned value is a
        // string which contains the entity id.
        $parent_field_value = $matches[1];
      }
      if (!empty($handler_settings['business_rules_view']['reference_parent_by_uuid'])) {
        $parent_field_value = static::convertEntityIdsToUuids($parent_field_value, $view
          ->getBaseEntityType()
          ->id());
      }

      // If we have an array with values we should implode those values and
      // enable Allow multiple values into our contextual filter.
      if (is_array($parent_field_value)) {
        $parent_field_value = implode(",", $parent_field_value);
      }

      // Get values from the view.
      $arguments = $handler_settings['business_rules_view']['arguments'];
      $view
        ->setArguments(!empty($parent_field_value) ? [
        $parent_field_value,
      ] + $arguments : $arguments);
      $view
        ->setDisplay($handler_settings['business_rules_view']['display_name']);
      $view
        ->preExecute();
      $view
        ->build();
      $options = static::getViewOptions($view);
      $form_field = $form[$child];
      $form_field['widget']['#options'] = $options;
      $html_field_id = explode('-wrapper-', $form_field['#id'])[0];

      // Fix html_field_id last char when it ends with _.
      $html_field_id = substr($child, strlen($child) - 1, 1) == '_' ? $html_field_id . '-' : $html_field_id;
      $formatter = $form_field['widget']['#type'];
      $response
        ->addCommand(new UpdateOptionsCommand($html_field_id, $options, $formatter));
    }
  }
  return $response;
}