InvokeCommand example

$parameters = $state->getOpenerParameters();
    if (empty($parameters['field_widget_id'])) {
      throw new \InvalidArgumentException('field_widget_id parameter is missing.');
    }

    // Create a comma-separated list of media IDs, insert them in the hidden     // field of the widget, and trigger the field update via the hidden submit     // button.     $widget_id = $parameters['field_widget_id'];
    $ids = implode(',', $selected_ids);
    $response
      ->addCommand(new InvokeCommand("[data-media-library-widget-value=\"$widget_id\"]", 'val', [$ids]))
      ->addCommand(new InvokeCommand("[data-media-library-widget-update=\"$widget_id\"]", 'trigger', ['mousedown']));

    return $response;
  }

}
$delta_to_focus = 0;
      foreach ($field_state['items'] as $delta => $item_fields) {
        $delta_to_focus = $delta;
        if ($item_fields['weight'] > $removed_item_weight) {
          // Stop directly when we find an item with a bigger weight. We also           // have to subtract 1 from the delta in this case, since the delta's           // are renumbered when rebuilding the form.           $delta_to_focus--;
          break;
        }
      }
      $response->addCommand(new InvokeCommand("#$wrapper_id [data-media-library-item-delta=$delta_to_focus]", 'focus'));
    }
    // Shift focus to the open button if the user removed the last selected     // item, or when the user has added items to the selection and is allowed to     // select more items. When 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 via JavaScript by adding the     // 'data-disabled-focus' attribute and we also don't want to set the focus     // here.     // @see Drupal.behaviors.MediaLibraryWidgetDisableButton     elseif ($removed_last || (!$is_remove_button && !isset($element['open_button']['#attributes']['data-disabled-focus']))) {
      $response->addCommand(new InvokeCommand("#$wrapper_id .js-media-library-open-button", 'focus'));
    }
// Find the delta of the next media item. If there is no item with a         // bigger delta, we automatically use the delta of the previous item and         // shift the focus there.         $removed_delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];
        $delta_to_focus = 0;
        foreach ($added_media as $delta => $media) {
          $delta_to_focus = $delta;
          if ($delta > $removed_delta) {
            break;
          }
        }
        $response->addCommand(new InvokeCommand("[data-media-library-added-delta=$delta_to_focus]", 'focus'));
      }
    }
    // Update the form and shift focus to the added media items.     else {
      $response->addCommand(new ReplaceCommand("#$wrapper_id", $form));
      $response->addCommand(new InvokeCommand('.js-media-library-add-form-added-media', 'focus'));
    }

    return $response;
  }

  
'data' => '<p>New Text!</p>',
      'settings' => ['my-setting' => 'setting'],
    ];

    $this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\InvokeCommand */
  public function testInvokeCommand() {
    $command = new InvokeCommand('#page-title', 'myMethod', ['var1', 'var2']);

    $expected = [
      'command' => 'invoke',
      'selector' => '#page-title',
      'method' => 'myMethod',
      'args' => ['var1', 'var2'],
    ];

    $this->assertEquals($expected$command->render());
  }

  
Home | Imprint | This part of the site doesn't use cookies.