You are here

wysiwyg_template.module in Wysiwyg API template plugin 8.2

Same filename and directory in other branches
  1. 7.2 wysiwyg_template.module
  2. 3.0.x wysiwyg_template.module

File

wysiwyg_template.module
View source
<?php

use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\node\Entity\NodeType;
use Drupal\wysiwyg_template\Entity\Template;

/**
 * Implements hook_editor_js_settings_alter().
 */
function wysiwyg_template_editor_js_settings_alter(array &$settings) {

  // Change url to be node-type-specific if creating or editing a node.
  $route_match = Drupal::routeMatch();
  $node_type = NULL;
  if ($route_match
    ->getRouteName() === 'entity.node.edit_form') {
    $node_type = $route_match
      ->getParameter('node')
      ->getType();
  }
  else {
    if ($route_match
      ->getRouteName() === 'node.add') {
      $node_type = $route_match
        ->getParameter('node_type')
        ->get('type');
    }
  }
  if ($node_type !== NULL) {
    foreach ($settings['editor']['formats'] as $format => $config) {
      if (isset($config['editorSettings']['templates_files'])) {
        $url = Url::fromRoute('wysiwyg_template.list_js.type', [
          'node_type' => $node_type,
        ]);
        $settings['editor']['formats'][$format]['editorSettings']['templates_files'] = [
          $url
            ->toString(),
        ];
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds WYSIWYG template selection as default node content.
 *
 * @see NodeTypeForm::form()
 */
function wysiwyg_template_form_node_type_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state
    ->getFormObject()
    ->getEntity();
  $templates = Template::loadByNodeType($type);
  $form['wysiwyg_template'] = [
    '#type' => 'details',
    '#title' => t('WYSIWYG template'),
    '#group' => 'additional_settings',
  ];
  $form['wysiwyg_template']['wysiwyg_template_default'] = [
    '#type' => 'select',
    '#options' => array_map(static function ($item) {
      return $item
        ->label();
    }, $templates),
    '#default_value' => $type
      ->getThirdPartySetting('wysiwyg_template', 'default_template'),
    '#title' => t('Default template for this content type'),
    '#empty_value' => '',
  ];
  $form['#entity_builders'][] = 'wysiwyg_template_form_node_type_form_builder';
}

/**
 * Entity builder for node type with WYSIWYG template options.
 *
 * @see wysiwyg_template_form_node_type_form_alter()
 */
function wysiwyg_template_form_node_type_form_builder($entity_type, NodeTypeInterface $type, array &$form, FormStateInterface $form_state) {
  $type
    ->setThirdPartySetting('wysiwyg_template', 'default_template', $form_state
    ->getValue('wysiwyg_template_default'));
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Set default content if configured to do so.
 */
function wysiwyg_template_form_node_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = NodeType::load($node
    ->getType());
  if ($node
    ->isNew() && ($template_id = $type
    ->getThirdPartySetting('wysiwyg_template', 'default_template'))) {

    /** @var \Drupal\wysiwyg_template_core\TemplateInterface $template */
    $template = Template::load($template_id);
    $form['body']['widget'][0]['#default_value'] = $template
      ->getBody();
  }
}