Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typos in hook_ckeditor_PLUGIN_plugin_check documentation #6821

Open
bennybobw opened this issue Jan 10, 2025 · 4 comments · May be fixed by backdrop/backdrop#4991
Open

Fix typos in hook_ckeditor_PLUGIN_plugin_check documentation #6821

bennybobw opened this issue Jan 10, 2025 · 4 comments · May be fixed by backdrop/backdrop#4991

Comments

@bennybobw
Copy link

Typos and errors in hook_ckeditor_PLUGIN_plugin_check documentation

in site/web/core/modules/ckeditor/ckeditor.api.php

function hook_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  $toolbars = $format->editor_settings['toolbar'];
  foreach ($toolbars as $toolbar['items'] as $row) {

    if (in_array('Underline', $row)) {
      return TRUE;
    }
  }
}

The foreach loop is completely wrong and also there are multiple $toolbars available. Additionally, using $row is confusing. $toolbar[$x] is the row but then there are button groups defined inside those, so I'd suggest renaming it to $group

Pull request incoming.

@avpaderno
Copy link
Member

avpaderno commented Jan 10, 2025

Actually, the code in core/modules/ckeditor/ckeditor.api.php is the following one.

function hook_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  // Automatically enable this plugin if the Underline button is enabled.
  foreach ($format->editor_settings['toolbar']['buttons'] as $row) {
    if (in_array('Underline', $row)) {
      return TRUE;
    }
  }
}

It does not work because in_array() checks the array values, not the array keys. in_array('Linux', $values) works when $values is array('Linux', 'macOS', 'Windows'), not array('Linux' => 1, 'macOS' => 2, 'Windows' => 3).

The code used in the PR would not work for the same reason.

function hook_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  $toolbars = $format->editor_settings['toolbar'];
  foreach ($toolbars as $toolbar) {
    foreach ($toolbar as $group) {
      if (in_array('Underline', $group['items'])) {
        return TRUE;
      }
    }
  }
}

@avpaderno
Copy link
Member

Also, core/modules/ckeditor5/ckeditor5.api.php contains the same code, for a similar hook (hook_ckeditor5_PLUGIN_plugin_check()).

The files that need to be changed are two.

@avpaderno
Copy link
Member

avpaderno commented Jan 10, 2025

The following code would work.

function hook_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  // Automatically enable this plugin if the Underline button is enabled.
  foreach ($format->editor_settings['toolbar']['buttons'] as $row) {
    if (in_array('Underline', array_keys($row))) {
      return TRUE;
    }
  }
}

That is how I would write the code, if I wanted to make the minimum change to fix it.

(I assume $format->editor_settings['toolbar']['buttons'] is correct to access the toolbar buttons.)

@avpaderno
Copy link
Member

Basing on the code used by ckeditor_get_settings(), I guess the correct code should be the following one.

function hook_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  // Automatically enable this plugin if the Underline button is enabled.
  foreach (foreach ($format->editor_settings['toolbar'] as $row) {
    foreach ($row as $button_group) {
      if (in_array('Underline', array_keys($button_group['items']))) {
        return TRUE;
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants