You are here

function n1ed_update_text_formats in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Updates text formats: attaches N1ED (or not).

2 calls to n1ed_update_text_formats()
n1ed_install in ./n1ed.install
Called when installed: attaches N1ED to appropriate formats.
n1ed_update_8215 in ./n1ed.install
Failsafe attaching N1ED on update from old version.

File

./n1ed.install, line 11
Installation hooks for N1ED module.

Code

function n1ed_update_text_formats($isInstall) {
  $textFormats = filter_formats();
  $n1edFormats = [];
  foreach ($textFormats as $textFormatName => $textFormat) {
    $editor = editor_load($textFormatName);
    if ($editor === NULL) {
      continue;
    }

    // no editor attached (i. e. in "Plain Text" format)
    $editorName = $editor
      ->get('editor');
    if ($editorName !== "ckeditor") {
      continue;
    }
    $filters = $textFormat
      ->get('filters');
    $filtersNames = array_keys($filters);
    $isAlaFullFormat = (!in_array("filter_html", $filtersNames) || $filters["filter_html"]["status"] != 1) && (!in_array("filter_html_escape", $filtersNames) || $filters["filter_html_escape"]["status"] != 1);
    if (($textFormatName === "full" || $textFormatName === "full_html") && !$isAlaFullFormat) {
      if (in_array("filter_html", $filtersNames)) {
        $filters["filter_html"]["status"] = FALSE;
      }
      if (in_array("filter_html_escape", $filtersNames)) {
        $filters["filter_html_escape"]["status"] = FALSE;
      }
      $textFormat
        ->set("filters", $filters);
      $textFormat
        ->save();
      $isAlaFullFormat = TRUE;
    }
    if ($isAlaFullFormat) {
      $n1edFormats[] = $textFormatName;
    }

    // Enable or disable N1ED for this format only:
    // - If we are installing module
    // - If we are updating module AND (disabling N1ED from format OR text
    //   format is "full[_html]")
    if ($isInstall || !$isInstall && (!$isAlaFullFormat || ($textFormatName === "full" || $textFormatName === "full_html"))) {
      $settings = $editor
        ->getSettings();
      if (!isset($settings["plugins"])) {
        $settings["plugins"] = [];
      }
      if (!isset($settings["plugins"]["N1EDEco"])) {
        $settings["plugins"]["N1EDEco"] = [];
      }
      $settings["plugins"]["N1EDEco"]["enableN1EDEcoSystem"] = $isAlaFullFormat ? 'true' : 'false';
      $editor
        ->setSettings($settings);
      $editor
        ->save();
    }
  }

  // Bubble N1ED text format up to allow use them in the first order
  // on the edit article page.
  $formats = [];
  foreach ($textFormats as $textFormatName => $textFormat) {
    if (in_array($textFormatName, $n1edFormats)) {
      $formats[] = $textFormat;
    }
  }
  $lastN1EDFormatIndex = count($formats) - 1;
  foreach ($textFormats as $textFormatName => $textFormat) {
    if (!in_array($textFormatName, $n1edFormats)) {
      $formats[] = $textFormat;
    }
  }
  do {
    $reorder = FALSE;
    for ($i = 1; $i < count($formats); $i++) {
      $format = $formats[$i];
      $weight = $format
        ->get("weight");
      $formatPrev = $formats[$i - 1];
      $weightPrev = $formatPrev
        ->get("weight");
      if ($weight < $weightPrev) {
        $format
          ->set("weight", $weightPrev);
        $formatPrev
          ->set("weight", $weight);
        $format
          ->save();
        $formatPrev
          ->save();
        $reorder = TRUE;
      }
    }
  } while ($reorder);
  if (count($formats) > $lastN1EDFormatIndex + 1 && $lastN1EDFormatIndex > -1 && $formats[$lastN1EDFormatIndex]
    ->get("weight") == $formats[$lastN1EDFormatIndex + 1]
    ->get("weight")) {
    for ($i = $lastN1EDFormatIndex + 1; $i < count($formats); $i++) {
      $formats[$i]
        ->set("weight", $formats[$i]
        ->get("weight") + 1);
      $formats[$i]
        ->save();
    }
  }
}