You are here

public static function BusinessRulesViewsSelection::updateDependentField in Business Rules 2.x

Same name and namespace in other branches
  1. 8 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 173

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();
  $subform = $form;
  $trigger_field = $form_state
    ->getTriggeringElement();

  // Inline form marker.
  $inline_form = FALSE;
  if (isset($trigger_field['#parents'])) {
    $parents = $trigger_field['#parents'];

    // If paragraphs are used, find the most deeply nested paragraph.
    while (TRUE) {
      if (!in_array('subform', $parents, TRUE)) {
        break;
      }
      $parent_field_key = array_shift($parents);

      /** @var \Drupal\field\Entity\FieldConfig $definition */
      $definition = $entity
        ->getFieldDefinition($parent_field_key);
      if ($definition
        ->getSetting('target_type') !== 'paragraph') {
        break;
      }
      $inline_form = TRUE;
      $delta = array_shift($parents);
      $widget = $subform[$parent_field_key]['widget'][$delta];
      if (!isset($widget['#paragraph_type'])) {
        break;
      }

      // Create a new paragraph instead of loading the actual paragraphs here,
      // as the actual paragraph can be empty.
      $entity = \Drupal::entityTypeManager()
        ->getStorage('paragraph')
        ->create([
        'type' => $widget['#paragraph_type'],
      ]);
      $subform = $subform[$parent_field_key]['widget'][$delta]['subform'];

      // Remove 'subform' corresponding with the current paragraph from array.
      array_shift($parents);
    }
  }

  // Update children.
  $children = $trigger_field['#ajax']['br_children'];
  $field_definition = $entity
    ->getFieldDefinitions();
  $response = new AjaxResponse();
  foreach ($children as $child) {
    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 the field widget is entity autocomplete, the returned value is a.
      if ($trigger_field['#type'] === 'entity_autocomplete' && preg_match('/\\((\\d )\\)$/', $parent_field_value, $matches)) {

        // 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 = $subform[$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'];

      // Check if field is multiple or not.
      $multiple = FALSE;

      /**
       * @var \Drupal\field\Entity\FieldStorageConfig $storage_config
       */
      $storage_config = $field_definition[$child]
        ->getFieldStorageDefinition();
      if ($storage_config
        ->getCardinality() === -1) {
        $multiple = TRUE;
      }
      $response
        ->addCommand(new UpdateOptionsCommand($html_field_id, $options, $formatter, $multiple));
    }
  }
  return $response;
}