getTriggeringElement example

'view' => $view->id(),
      'display_id' => $new_display_id,
    ]);
  }

  /** * Submit handler to add a display to a view. */
  public function submitDisplayAdd($form, FormStateInterface $form_state) {
    $view = $this->entity;
    // Create the new display.     $parents = $form_state->getTriggeringElement()['#parents'];
    $display_type = array_pop($parents);
    $display = $view->getExecutable()->newDisplay($display_type);
    $display_id = $display->display['id'];
    // A new display got added so the asterisks symbol should appear on the new     // display.     $view->getExecutable()->current_display = $display_id;
    $view->cacheSet();

    // Redirect to the new display's edit page.     $form_state->setRedirect('entity.view.edit_display_form', [
      'view' => $view->id(),
      
$field_state = static::getWidgetState($parents$field_name$form_state);
    $field_state['array_parents'] = $element['#array_parents'];
    static::setWidgetState($parents$field_name$form_state$field_state);

    return $element;
  }

  /** * Submission handler for the "Add another item" button. */
  public static function addMoreSubmit(array $form, FormStateInterface $form_state) {
    $button = $form_state->getTriggeringElement();

    // Go one level up in the form, to the widgets container.     $element = NestedArray::getValue($formarray_slice($button['#array_parents'], 0, -1));
    $field_name = $element['#field_name'];
    $parents = $element['#field_parents'];

    // Increment the items count.     $field_state = static::getWidgetState($parents$field_name$form_state);
    $field_state['items_count']++;
    static::setWidgetState($parents$field_name$form_state$field_state);

    

      }
    }

    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($triggering_element = $form_state->getTriggeringElement()) {
      $this->messenger()->addStatus(t('The clicked button is %name.', ['%name' => $triggering_element['#name']]));
    }
    else {
      $this->messenger()->addStatus('There is no clicked button.');
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    
// Allow the media library to be opened even if there are form errors.       '#limit_validation_errors' => [],
    ];

    // When the user returns from the modal to the widget, we want to shift the     // focus back to the open button. If the user is not allowed to add more     // items, the button needs to be disabled. Since we can't shift the focus to     // disabled elements, the focus is set back to the open button via     // JavaScript by adding the 'data-disabled-focus' attribute.     // @see Drupal.behaviors.MediaLibraryWidgetDisableButton     if (!$cardinality_unlimited && $remaining === 0) {
      $triggering_element = $form_state->getTriggeringElement();
      if ($triggering_element && ($trigger_parents = $triggering_element['#array_parents']) && end($trigger_parents) === 'media_library_update_widget') {
        // The widget is being rebuilt from a selection change.         $element['open_button']['#attributes']['data-disabled-focus'] = 'true';
        $element['open_button']['#attributes']['class'][] = 'visually-hidden';
      }
      else {
        // The widget is being built without a selection change, so we can just         // set the item to disabled now, there is no need to set the focus         // first.         $element['open_button']['#disabled'] = TRUE;
        $element['open_button']['#attributes']['class'][] = 'visually-hidden';
      }
/** * Submit handler for the remove button. * * @param array $form * The form render array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. */
  public function removeButtonSubmit(array $form, FormStateInterface $form_state) {
    // Retrieve the delta of the media item from the parents of the remove     // button.     $triggering_element = $form_state->getTriggeringElement();
    $delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];

    $added_media = $form_state->get('media');
    $removed_media = $added_media[$delta];

    // Update the list of added media items in the form state.     unset($added_media[$delta]);

    // Update the media items in the form state.     $form_state->set('media', $added_media)->setRebuild();

    
/** * Submit handler for the remove button. * * @param array $form * The form render array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. */
  public function removeButtonSubmit(array $form, FormStateInterface $form_state) {
    // Retrieve the delta of the media item from the parents of the remove     // button.     $triggering_element = $form_state->getTriggeringElement();
    $delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];

    /** @var \Drupal\media\MediaInterface $removed_media */
    $removed_media = $form_state->get(['media', $delta]);

    $file = $removed_media->get($this->getSourceFieldName($removed_media->bundle->entity))->entity;
    if ($file instanceof FileInterface && empty($this->fileUsage->listUsage($file))) {
      $file->delete();
    }

    parent::removeButtonSubmit($form$form_state);
  }


  /** * Validate callback for alt and title field, if the user wants them required. * * This is separated in a validate function instead of a #required flag to * avoid being validated on the process callback. */
  public static function validateRequiredFields($element, FormStateInterface $form_state) {
    // Only do validation if the function is triggered from other places than     // the image process form.     $triggering_element = $form_state->getTriggeringElement();
    if (!empty($triggering_element['#submit']) && in_array('file_managed_file_submit', $triggering_element['#submit'], TRUE)) {
      $form_state->setLimitValidationErrors([]);
    }
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    $style_id = $this->getSetting('preview_image_style');
    
/** * {@inheritdoc} */
  public function setTriggeringElement($triggering_element) {
    $this->triggering_element = $triggering_element;
    return $this;
  }

  /** * {@inheritdoc} */
  public function DgetTriggeringElement() {
    return $this->triggering_element;
  }

  /** * {@inheritdoc} */
  public function setValidateHandlers(array $validate_handlers) {
    $this->validate_handlers = $validate_handlers;
    return $this;
  }

  

  public function setTriggeringElement($triggering_element);

  /** * Gets the form element that triggered submission. * * @return array|null * The form element that triggered submission, of NULL if there is none. */
  public function DgetTriggeringElement();

  /** * Sets the validate handlers. * * @param array $validate_handlers * An array of validate handlers. * * @return $this */
  public function setValidateHandlers(array $validate_handlers);

  
// Required for #limit_validation_errors.       '#submit' => [[$this, 'submitForm']],
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Verify that the only submit button was recognized as triggering_element.     $this->assertEquals($form['actions']['submit']['#array_parents']$form_state->getTriggeringElement()['#array_parents']);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /** * Tests that #limit_validation_errors of the only submit button takes effect. */
  

  protected function handleErrorsWithLimitedValidation(&$form, FormStateInterface &$form_state$form_id) {
    // If validation errors are limited then remove any non validated form values,     // so that only values that passed validation are left for submit callbacks.     $triggering_element = $form_state->getTriggeringElement();
    if (isset($triggering_element['#limit_validation_errors']) && $triggering_element['#limit_validation_errors'] !== FALSE) {
      $values = [];
      foreach ($triggering_element['#limit_validation_errors'] as $section) {
        // If the section exists within $form_state->getValues(), even if the         // value is NULL, copy it to $values.         $section_exists = NULL;
        $value = NestedArray::getValue($form_state->getValues()$section$section_exists);
        if ($section_exists) {
          NestedArray::setValue($values$section$value);
        }
      }
      

  public static function submit($form, FormStateInterface $form_state) {
    // During the form rebuild, formElement() will create field item widget     // elements using re-indexed deltas, so clear out FormState::$input to     // avoid a mismatch between old and new deltas. The rebuilt elements will     // have #default_value set appropriately for the current state of the field,     // so nothing is lost in doing this.     $button = $form_state->getTriggeringElement();
    $parents = array_slice($button['#parents'], 0, -2);
    NestedArray::setValue($form_state->getUserInput()$parents, NULL);

    // Go one level up in the form, to the widgets container.     $element = NestedArray::getValue($formarray_slice($button['#array_parents'], 0, -1));
    $field_name = $element['#field_name'];
    $parents = $element['#field_parents'];

    $submitted_values = NestedArray::getValue($form_state->getValues()array_slice($button['#parents'], 0, -2));
    foreach ($submitted_values as $delta => $submitted_value) {
      if (empty($submitted_value['fids'])) {
        

  public static function updateWidget(array &$form, FormStateInterface $form_state, Request $request) {
    $field_id = $form_state->getTriggeringElement()['#field_id'];
    $selected_ids = $form_state->getValue($field_id);
    $selected_ids = $selected_ids ? array_filter(explode(',', $selected_ids)) : [];

    // Allow the opener service to handle the selection.     $state = MediaLibraryState::fromRequest($request);

    $current_selection = $form_state->getValue('media_library_select_form_selection');
    $available_slots = $state->getAvailableSlots();
    $selected_count = count(explode(',', $current_selection));
    if ($available_slots > 0 && $selected_count > $available_slots) {
      $response = new AjaxResponse();
      
// If we don't already have a file, there is nothing to remove.     else {
      $element['remove_button']['#access'] = FALSE;
    }
    return $element;
  }

  /** * Render API callback: Validates the managed_file element. */
  public static function validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
    $triggering_element = $form_state->getTriggeringElement();
    $clicked_button = isset($triggering_element['#parents']) ? end($triggering_element['#parents']) : '';
    if ($clicked_button != 'remove_button' && !empty($element['fids']['#value'])) {
      $fids = $element['fids']['#value'];
      foreach ($fids as $fid) {
        if ($file = File::load($fid)) {
          // If referencing an existing file, only allow if there are existing           // references. This prevents unmanaged files from being deleted if           // this item were to be deleted. When files that are no longer in use           // are automatically marked as temporary (now disabled by default),           // it is not safe to reference a permanent file without usage. Adding           // a usage and then later on removing it again would delete the file,


    // Use PHPUnit for mocking, because Prophecy cannot mock methods that return     // by reference. See \Prophecy\Doubler\Generator\Node::getCode().     $decorated_form_state = $this->createMock(FormStateInterface::class);
    $decorated_form_state->expects($this->once())
      ->method('getTriggeringElement')
      ->willReturn($triggering_element);

    $this->formStateDecoratorBase = new NonAbstractFormStateDecoratorBase($decorated_form_state);

    $this->assertSame($triggering_element$this->formStateDecoratorBase->getTriggeringElement());
  }

  /** * @covers ::setValidateHandlers */
  public function testSetValidateHandlers() {
    $validate_handlers = [
      'FOO' => 'BAR',
    ];

    $this->decoratedFormState->setValidateHandlers($validate_handlers)
      
Home | Imprint | This part of the site doesn't use cookies.