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

Call plugin callbacks in the order they are configured #1285

Merged
merged 9 commits into from
Mar 4, 2022

Conversation

Vainonen
Copy link
Contributor

@Vainonen Vainonen commented Mar 2, 2022

Reasons for creating this PR

Fixes reopened issue #1148. Orders also callback names of plugins in the same order as sorted in VocabularyConfig.php and configured in skosmos:vocabularyPlugins.

Link to relevant issue(s), if any

Description of the changes in this PR

Plugin order is configured with skosmos:vocabularyPlugins. PR #1201 made it possible to define order of plugins in configuration. PluginRegister class reorders the plugins in alphabetical order of plugin file names, but this fix rearranges the callback names to the same order as in skosmos:vocabularyPlugins. PR also adds the possibility to define parameter plugins outside of the ordered plugin list

Known problems or uncertainties in this PR

Plugin rendering order is reverse than the order configured in skosmos:vocabularyPlugins. To solve this, Skosmos HTML template could have additional empty div element (e.g. <div id="concept-info-after"></div>), which serves as a location for widgets. Each widget could then reserve own empty div element during callback with JQuery $('#concept-info-after').append(...). Then the rendering would be done in same order as in skosmos:vocabularyPlugins.

In same way a similar div element (e.g. <div id="concept-info-before">) could be added for widgets with message (e.g. https://github.com/NatLibFi/Skosmos-widget-message) above the concept info.

@Vainonen Vainonen added this to the 2.14 milestone Mar 2, 2022
@Vainonen Vainonen self-assigned this Mar 2, 2022
@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #1285 (9958dbb) into master (841dc1b) will increase coverage by 0.98%.
The diff coverage is 92.30%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1285      +/-   ##
============================================
+ Coverage     69.33%   70.32%   +0.98%     
- Complexity     1648     1685      +37     
============================================
  Files            32       32              
  Lines          4047     4195     +148     
============================================
+ Hits           2806     2950     +144     
- Misses         1241     1245       +4     
Impacted Files Coverage Δ
model/PluginRegister.php 72.61% <89.47%> (+2.46%) ⬆️
model/ConceptSearchParameters.php 85.55% <100.00%> (+1.11%) ⬆️
model/VocabularyConfig.php 96.18% <100.00%> (+0.05%) ⬆️
model/sparql/GenericSparql.php 93.02% <0.00%> (+0.78%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 841dc1b...9958dbb. Read the comment docs.

Copy link
Member

@osma osma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use the PR template to describe the changes in this PR?

Also, it would be helpful to provide a minimal example of how to test this. For example a test configuration with 2-3 plugins that were in the wrong order before but are correctly ordered with the changes in this PR.

Copy link
Member

@osma osma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR introduction is now good. Here are some suggestions for changes:

  • if it's easy, make the tests cover the case of non-null $names parameter for getPluginCallbacks (see suggestions in comments)
  • the <script> elements for plugins in HTML code should be in the configuration order of plugins
  • there should be concept-info-before and concept-info-after elements in concept-shared.twig, like this:

Around line 15:

    <div id="concept-info-before"></div>
    <div class="concept-info{% if concept.deprecated %} deprecated-concept{% endif %}">

Around line 211:

      </div>
    </div>
    <div id="concept-info-after"></div>
    {% endfor %}

* @return array
*/
public function getPluginCallbacks($names=null) {
if ($names) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests don't cover the case of $names being passed. I think it should be easy to modify the test for getPluginCallbacks. I will suggest below...

{
$plugins = new PluginRegister();
$this->assertEquals(array('test-plugin' => array('plugins/test-plugin/stylesheet.css')), $this->mockpr->getPluginsCSS(array('test-plugin')));
$this->assertEquals($this->mockpr->getPluginCallbacks()['test-plugin1'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here passing a $names parameter to getPluginCallbacks would increase test coverage.

@sonarcloud
Copy link

sonarcloud bot commented Mar 4, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@osma
Copy link
Member

osma commented Mar 4, 2022

The Javascript execution order is now good.

The new <div> elements in the Twig template seem to be working as well. I was able to create a set of three plugins which insert themselves into the provided areas.

I added some annotations to increase test coverage. The case of non-empty $names parameter is still not covered, but I think we can live with that.

The documentation for plugins still needs to be updated. I think we need updated guidelines on how plugins should insert their elements into the DOM structure, namely:

  1. The callback function of the plugin SHOULD append (insert as last) an element for the plugin into one of the provided DIVs (or elsewhere on the page). It can be an empty placeholder DIV with an id that identifies it to the plugin. For example like this: $('#concept-info-after').append('<div id="my-widget"></div>');
  2. If the plugin performs async operations, for example calls an external API, it can update its DIV once the API call has completed and all information is available. As a minimal example, they can call $('#my-widget').text(results); where results is something that was returned by the external API call.

This way the order of the inserted elements remains the same as the configured order of plugins (which is the order for the function calls to the plugin callbacks). It doesn't matter how long async operations take, because the order has been established by the placeholder elements and won't change even if they are updated.

I will merge this PR as it does its job; updating the documentation for plugins still remains as a task.

@osma osma merged commit 958e067 into master Mar 4, 2022
@osma osma deleted the issue1148-order-of-plugin-rendering branch March 4, 2022 16:26
@osma osma changed the title Issue1148 order of plugin rendering Call plugin callbacks in the order they are configured Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Order of vocabulary-specific plugins is not configurable
3 participants