DataCommand.php 1.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

/**
 * @file
 * Definition of Drupal\Core\Ajax\DataCommand.
 */

namespace Drupal\Core\Ajax;

use Drupal\Core\Ajax\CommandInterface;

/**
 * An AJAX command for implementing jQuery's data() method.
 *
 * This instructs the client to attach the name=value pair of data to the
 * selector via jQuery's data cache.
 *
18
 * This command is implemented by Drupal.AjaxCommands.prototype.data() defined
19
 * in misc/ajax.js.
20 21
 *
 * @ingroup ajax
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */
class DataCommand implements CommandInterface {

  /**
   * A CSS selector string for elements to which data will be attached.
   *
   * If the command is a response to a request from an #ajax form element then
   * this value can be NULL.
   *
   * @var string
   */
  protected $selector;

  /**
   * The key of the data attached to elements matched by the selector.
   *
   * @var string
   */
  protected $name;

  /**
43
   * The value of the data to be attached to elements matched by the selector.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   *
   * The data is not limited to strings; it can be any format.
   *
   * @var mixed
   */
  protected $value;

  /**
   * Constructs a DataCommand object.
   *
   * @param string $selector
   *   A CSS selector for the elements to which the data will be attached.
   * @param string $name
   *   The key of the data to be attached to elements matched by the selector.
   * @param type $value
   *   The value of the data to be attached to elements matched by the selector.
   */
  public function __construct($selector, $name, $value) {
    $this->selector = $selector;
    $this->name = $name;
    $this->value = $value;
  }

  /**
   * Implements Drupal\Core\Ajax\CommandInterface:render().
   */
  public function render() {

    return array(
      'command' => 'data',
      'selector' => $this->selector,
      'name' => $this->name,
      'value' => $this->value,
    );
  }

}