FANDOM


  • Hi there! I'm new to Fandom coding, so I'm still getting a handle of the basics. :)

    I want to make a check, each time a page edit is saved, if the page that was just edited has a certain template on it. I'm not really sure how to do that... Since there is not a good search functions on the Dev Wiki and the documentation is useful only if you know which page you want, I don't know where to look for this.

    I've got this so far:

     mw.hook('postEdit').add(function($content) {
         // Check if template X is on the page  
     });
    

    If you can be gentle and give lots of explanations with your answer, I'd really appreciate it. Thanks so much for the help!

      Loading editor
    • You'll have to call the MediaWiki API to check whether the page uses a certain template. Something along the lines of:

      mw.loader.using('mediawiki.api').then(function() {
          return new mw.Api().get({
              action: 'query',
              pageids: mw.config.get('wgArticleId'),
              prop: 'templates'
          });
      }).then(function(data) {
          if (data.error) {
              console.log('An error occurred', data.error.code);
          } else {
              console.log(
                  data.query
                      .pages[mw.config.get('wgArticleId')]
                      .templates
                      .filter(function(template) {
                          return template.title === 'Template:Templatename';
                      })
                      .length === 0 ?
                          'There is no such template on the page' :
                          'The template is in the page'
              );
          }
      });

      This is using MediaWiki's mw.Api class to call the prop=templates API endpoint.

        Loading editor
    • A FANDOM user
        Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+