You are here

n1ed.module in N1ED - Visual editor as CKEditor plugin with Bootstrap support 7

Same filename and directory in other branches
  1. 8.2 n1ed.module
  2. 8 n1ed.module

File

n1ed.module
View source
<?php

/**
 * @file
 * Main code for N1ED module.
 */
require 'vendor/autoload.php';
use EdSDK\FlmngrServer\FlmngrServer;

/**
 * Implementation of hook_menu
 */
function n1ed_menu() {
  $items = [];
  $items['admin/config/content/n1ed'] = [
    'title' => 'N1ED configuration',
    'description' => 'N1ED settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'n1ed_admin_form',
    ],
    'access arguments' => [
      'administer users',
    ],
    'type' => MENU_NORMAL_ITEM,
  ];
  $items['admin/config/content/n1ed/setApiKey'] = [
    'title' => 'setApikey',
    'page callback' => 'n1ed_setApiKey',
    'access arguments' => [
      'administer users',
    ],
  ];
  $items['admin/config/content/n1ed/flmngr'] = [
    'title' => 'FM',
    'page callback' => 'n1ed_flmngr',
    'access arguments' => [
      'administer users',
    ],
  ];
  return $items;
}

/**
 * Handler for Flmngr requests
 */
function n1ed_flmngr() {
  $tmp = drupal_get_path('module', 'n1ed') . '/tmp/';
  $cache = drupal_get_path('module', 'n1ed') . '/cache/';
  if (!file_exists($tmp)) {
    mkdir($tmp, 0777, TRUE);
  }
  if (!file_exists($cache)) {
    mkdir($cache, 0777, TRUE);
  }
  if (!file_exists($cache)) {
    mkdir(drupal_realpath('public://') . '/flmngr', 0777, TRUE);
  }
  echo FlmngrServer::flmngrRequest([
    'dirFiles' => drupal_realpath('public://') . '/flmngr',
    'dirTmp' => $tmp,
    'dirCache' => $cache,
  ]);
}

/**
 * Handler for setting API key and access token for N1ED
 */
function n1ed_setApiKey() {
  $key = $_POST['n1edApiKey'];
  $token = $_POST['n1edToken'];
  variable_set('n1edApiKey', $key);
  variable_set('n1edToken', $token);
  die('ok');
}

/**
 * Implemnetation of hook_form_alter
 */
function n1ed_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "article_node_form") {
    $form['body'][LANGUAGE_NONE][0]['summary']['#attributes']['data-disable-n1ed'] = 'true';
  }
}

/**
 * Removing 'iframe' plugin due to N1ED handles IFrames better and will conflict with default add-on
 */
function n1ed_ckeditor_settings_alter(array &$settings, &$conf) {
  $settings['removePlugins'] = 'iframe';
  $conf['removePlugins'] = 'iframe';
}

/**
 * Implementation of hook_preprocess_page
 */
function n1ed_preprocess_page(&$page) {
  if ($page['page']['content']['system_main'] && @$page['page']['content']['system_main']['#node_edit_form']) {
    if (!variable_get('n1edApiKey')) {
      variable_set('n1edApiKey', 'N1D7DFLT');
    }
    $version = variable_get('n1ed_version') ?: '';
    if ($version === '') {
      $version = 'latest';
    }
    $urlCache = variable_get('urlCache') ?: '';
    if ($urlCache === '') {

      // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
      // Do not start URL with "https:" prefix.
      // Notice about cookies: developers use it to specify debug server to use,.
      // All other users will use old known cloud.n1ed.com address.
      $path = '//' . (isset($_COOKIE["N1ED_PREFIX"]) ? $_COOKIE["N1ED_PREFIX"] . "." : "") . 'cloud.n1ed.com/cdn/' . variable_get('n1edApiKey') . '/' . $version . '/ckeditor/plugins/N1EDEco/plugin.js';
    }
    else {

      // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
      // Do not start URL with "https:" prefix.
      if (strpos($urlCache, "http:") === 0) {
        $urlCache = substr($urlCache, 5);
      }
      elseif (strpos($urlCache, "https:") === 0) {
        $urlCache = substr($urlCache, 6);
      }
      $path = $urlCache . variable_get('n1edApiKey') . '/' . $version . '/ckeditor/plugins/N1EDEco/plugin.js';
    }
    drupal_add_js([
      'N1ED' => [
        'urlPlugin' => $path,
        'urlFileManager' => '/admin/config/content/n1ed/flmngr',
        'urlFiles' => '/' . variable_get('file_public_path', conf_path() . '/files/'),
        'defaultUploadDir' => '/uploads/',
      ],
    ], 'setting');
  }
}

/**
 * Implementation of hook_ckeditor_plugin
 */
function n1ed_ckeditor_plugin() {
  return [
    'n1ed' => [
      'name' => 'N1ED',
      'desc' => t('N1ED - visual editor with Bootstrap support'),
      'path' => drupal_get_path('module', 'n1ed') . '/plugins/n1ed/',
    ],
  ];
}

/**
 * Admin form to configurable welcome message.
 */
function n1ed_admin_form($form, &$form_state) {
  $adminJS = '/' . drupal_get_path('module', 'n1ed') . '/js/admin.js';
  if (!variable_get('n1edApiKey')) {
    variable_set('n1edApiKey', 'N1D7DFLT');
  }
  drupal_add_js($adminJS, 'external');
  $form['#attributes'] = [
    'data-apikey' => variable_get('n1edApiKey'),
    'data-token' => variable_get('n1edToken'),
  ];
  $form['n1ed_config'] = [
    '#type' => 'hidden',
    '#required' => FALSE,
  ];
  return system_settings_form($form);
}

Functions

Namesort descending Description
n1ed_admin_form Admin form to configurable welcome message.
n1ed_ckeditor_plugin Implementation of hook_ckeditor_plugin
n1ed_ckeditor_settings_alter Removing 'iframe' plugin due to N1ED handles IFrames better and will conflict with default add-on
n1ed_flmngr Handler for Flmngr requests
n1ed_form_alter Implemnetation of hook_form_alter
n1ed_menu Implementation of hook_menu
n1ed_preprocess_page Implementation of hook_preprocess_page
n1ed_setApiKey Handler for setting API key and access token for N1ED