Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. InsertCommand.php

class InsertCommand

Generic AJAX command for inserting content.

This command instructs the client to insert the given render array or HTML using whichever jQuery DOM manipulation method has been specified in the #ajax['method'] variable of the element that triggered the request.

This command is implemented by Drupal.AjaxCommands.prototype.insert() defined in misc/ajax.js.

Hierarchy

  • class \Drupal\Core\Ajax\InsertCommand implements \Drupal\Core\Ajax\CommandInterface, \Drupal\Core\Ajax\CommandWithAttachedAssetsInterface uses \Drupal\Core\Ajax\CommandWithAttachedAssetsTrait

Expanded class hierarchy of InsertCommand

Related topics

Ajax API
Overview for Drupal's Ajax API.
3 files declare their use of InsertCommand
AjaxRenderer.php in core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
ListItemBase.php in core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
WidgetBase.php in core/lib/Drupal/Core/Field/WidgetBase.php

File

core/lib/Drupal/Core/Ajax/InsertCommand.php, line 17

Namespace

Drupal\Core\Ajax
View source
class InsertCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
    use CommandWithAttachedAssetsTrait;
    
    /**
     * A CSS selector string.
     *
     * If the command is a response to a request from an #ajax form element then
     * this value can be NULL.
     *
     * @var string|null
     */
    protected $selector;
    
    /**
     * The content for the matched element(s).
     *
     * Either a render array or an HTML string.
     *
     * @var string|array
     */
    protected $content;
    
    /**
     * A settings array to be passed to any attached JavaScript behavior.
     *
     * @var array
     */
    protected $settings;
    
    /**
     * Constructs an InsertCommand object.
     *
     * @param string|null $selector
     *   A CSS selector.
     * @param string|array $content
     *   The content that will be inserted in the matched element(s), either a
     *   render array or an HTML string.
     * @param array $settings
     *   An array of JavaScript settings to be passed to any attached behaviors.
     */
    public function __construct($selector, $content, ?array $settings = NULL) {
        $this->selector = $selector;
        $this->content = $content;
        $this->settings = $settings;
    }
    
    /**
     * Implements Drupal\Core\Ajax\CommandInterface:render().
     */
    public function render() {
        return [
            'command' => 'insert',
            'method' => NULL,
            'selector' => $this->selector,
            'data' => $this->getRenderedContent(),
            'settings' => $this->settings,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CommandWithAttachedAssetsTrait::$attachedAssets protected property The attached assets for this Ajax command.
CommandWithAttachedAssetsTrait::getAttachedAssets public function Gets the attached assets.
CommandWithAttachedAssetsTrait::getRenderedContent protected function Processes the content for output.
InsertCommand::$content protected property The content for the matched element(s).
InsertCommand::$selector protected property A CSS selector string.
InsertCommand::$settings protected property A settings array to be passed to any attached JavaScript behavior.
InsertCommand::render public function Implements Drupal\Core\Ajax\CommandInterface:render(). Overrides CommandInterface::render 6
InsertCommand::__construct public function Constructs an InsertCommand object.
RSS feed
Powered by Drupal