Editor.php 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\editor\Plugin\Core\Entity\Editor.
 */

namespace Drupal\editor\Plugin\Core\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
11
use Drupal\Core\Entity\Annotation\EntityType;
12 13 14 15 16
use Drupal\Core\Annotation\Translation;

/**
 * Defines the configured text editor entity.
 *
17
 * @EntityType(
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 82 83 84
 *   id = "editor",
 *   label = @Translation("Editor"),
 *   module = "editor",
 *   controller_class = "Drupal\Core\Config\Entity\ConfigStorageController",
 *   config_prefix = "editor.editor",
 *   entity_keys = {
 *     "id" = "format",
 *     "uuid" = "uuid"
 *   }
 * )
 */
class Editor extends ConfigEntityBase {

  /**
   * The machine name of the text format with which this configured text editor
   * is associated.
   *
   * @var string
   */
  public $format;

  /**
   * The name (plugin ID) of the text editor.
   *
   * @var string
   */
  public $editor;

  /**
   * The array of settings for the text editor.
   *
   * @var array
   */
  public $settings = array();

  /**
   * Overrides Drupal\Core\Entity\Entity::id().
   */
  public function id() {
    return $this->format;
  }

  /**
   * Overrides Drupal\Core\Entity\Entity::label().
   */
  public function label($langcode = NULL) {
    $format = filter_format_load($this->format);
    return $format->name;
  }

  /**
   * Overrides Drupal\Core\Entity\Entity::__construct()
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);

    $manager = drupal_container()->get('plugin.manager.editor');
    $plugin = $manager->createInstance($this->editor);

    // Initialize settings, merging module-provided defaults.
    $default_settings = $plugin->getDefaultSettings();
    $default_settings += module_invoke_all('editor_default_settings', $this->editor);
    drupal_alter('editor_default_settings', $default_settings, $this->editor);
    $this->settings += $default_settings;
  }

}