You are here

class Gutenberg in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Editor/Gutenberg.php \Drupal\gutenberg\Plugin\Editor\Gutenberg

Defines a Gutenberg-based text editor for Drupal.

Plugin annotation


@Editor(
  id = "gutenberg",
  label = @Translation("Gutenberg"),
  supports_content_filtering = TRUE,
  supports_inline_editing = TRUE,
  is_xss_safe = FALSE,
  supported_element_types = {
    "textarea"
  }
)

Hierarchy

Expanded class hierarchy of Gutenberg

3 string references to 'Gutenberg'
example_block.info.yml in modules/example_block/example_block.info.yml
modules/example_block/example_block.info.yml
gutenberg.info.yml in ./gutenberg.info.yml
gutenberg.info.yml
gutenberg_cloud.info.yml in modules/gutenberg_cloud/gutenberg_cloud.info.yml
modules/gutenberg_cloud/gutenberg_cloud.info.yml

File

src/Plugin/Editor/Gutenberg.php, line 31

Namespace

Drupal\gutenberg\Plugin\Editor
View source
class Gutenberg extends EditorBase implements ContainerFactoryPluginInterface {

  /**
   * The module handler to invoke hooks on.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The Gutenberg plugin manager.
   *
   * @var \Drupal\gutenberg\GutenbergPluginManager
   */
  protected $gutenbergPluginManager;

  /**
   * Drupal\Core\Render\RendererInterface instance.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a Gutenberg object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\gutenberg\GutenbergPluginManager $gutenberg_plugin_manager
   *   The Gutenberg plugin manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks on.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, GutenbergPluginManager $gutenberg_plugin_manager, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, RendererInterface $renderer) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->gutenbergPluginManager = $gutenberg_plugin_manager;
    $this->moduleHandler = $module_handler;
    $this->languageManager = $language_manager;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.gutenberg.plugin'), $container
      ->get('module_handler'), $container
      ->get('language_manager'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultSettings() {
    return [
      'plugins' => [
        'language' => [
          'language_list' => 'un',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {

    // Gutenberg plugin settings, if any.
    $form['plugin_settings'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Gutenberg plugin settings'),
      '#attributes' => [
        'id' => 'gutenberg-plugin-settings',
      ],
    ];
    $this->gutenbergPluginManager
      ->injectPluginSettingsForm($form, $form_state, $editor);
    if (count(Element::children($form['plugins'])) === 0) {
      unset($form['plugins']);
      unset($form['plugin_settings']);
    }
    return $form;
  }

  /**
   * Returns a list of language codes supported by CKEditor.
   *
   * @return array
   *   An associative array keyed by language codes.
   */
  public function getLangcodes() {
    return [
      'en' => 'en',
    ];
  }

  /**
   * Get javascript settings.
   *
   * @param \Drupal\editor\Entity\Editor $editor
   *   A configured text editor object.
   */
  public function getJsSettings(Editor $editor) {
    $config = \Drupal::service('config.factory')
      ->getEditable('gutenberg.settings');
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    if (!$node) {
      $route_match = \Drupal::service('current_route_match');
      if (!$route_match
        ->getParameter('node_type')) {
        return;
      }
      $node_type = $route_match
        ->getParameter('node_type')
        ->get('type');
    }
    else {
      $node_type = $node->type
        ->getString();
    }
    $blocks_settings = UtilsController::getBlocksSettings();
    $settings = [
      'contentType' => $node_type,
      'allowedBlocks' => $config
        ->get($node_type . '_allowed_blocks'),
      'blackList' => $blocks_settings['blacklist'],
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
    $libraries = [
      'gutenberg/edit-node',
      // Media attributes overrides must come after all
      // Gutenberg initialization.
      'gutenberg/g-media-attributes',
      'gutenberg/blocks-edit',
      'gutenberg/drupal-blocks',
    ];
    return $libraries;
  }

  /**
   * Builds the "toolbar" configuration part of the CKEditor JS settings.
   *
   * @param \Drupal\editor\Entity\Editor $editor
   *   A configured text editor object.
   *
   * @return array
   *   An array containing the "toolbar" configuration.
   *
   * @see getJsSettings()
   */
  public function buildToolbarJsSetting(Editor $editor) {
    $toolbar = [];
    return $toolbar;
  }

  /**
   * Builds the "contentsCss" configuration part of the CKEditor JS settings.
   *
   * @param \Drupal\editor\Entity\Editor $editor
   *   A configured text editor object.
   *
   * @return array
   *   An array containing the "contentsCss" configuration.
   *
   * @see getJsSettings()
   */
  public function buildContentsCssJsSetting(Editor $editor) {
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditorBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 3
EditorBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
EditorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
EditorPluginInterface::getJSSettings public function Returns JavaScript settings to be attached. 4
Gutenberg::$gutenbergPluginManager protected property The Gutenberg plugin manager.
Gutenberg::$languageManager protected property The language manager.
Gutenberg::$moduleHandler protected property The module handler to invoke hooks on.
Gutenberg::$renderer protected property Drupal\Core\Render\RendererInterface instance.
Gutenberg::buildContentsCssJsSetting public function Builds the "contentsCss" configuration part of the CKEditor JS settings.
Gutenberg::buildToolbarJsSetting public function Builds the "toolbar" configuration part of the CKEditor JS settings.
Gutenberg::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Gutenberg::getDefaultSettings public function Returns the default settings for this configurable text editor. Overrides EditorBase::getDefaultSettings
Gutenberg::getJsSettings public function Get javascript settings.
Gutenberg::getLangcodes public function Returns a list of language codes supported by CKEditor.
Gutenberg::getLibraries public function Returns libraries to be attached. Overrides EditorPluginInterface::getLibraries
Gutenberg::settingsForm public function
Gutenberg::__construct public function Constructs a Gutenberg object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.