Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
391 views
in Plugins by
edited by

I've been writing an editor plugin. In its get_field(&$qa_content, $content, $format, $fieldname, $rows, $autofocus) function I need to know whether the current request is a question editing page.

How do I do this? It'd be nice if the code doesn't break on future Q2A updates.

 

 

Note that qa_request() just gives the question number and title, no specification as to whether the page is in edit mode.

Q2A version: 1.6.3

2 Answers

+2 votes
by
selected by
 
Best answer

This should be enough to check whether you're editing: strpos(qa_get_state(), 'edit') === 0

As the template is accessible from the theme or from a layer plugin, you can't use it. The closest thing to check whether you are in a question template or not would be to check this: isset($qa_content['q_view'])

So the final code would look like this:

if (isset($qa_content['q_view']) && strpos(qa_get_state(), 'edit') === 0) {
    // Magic happens in here
}

 

by
as always, thanks a billion pupi1985 :)
by
one thing, the latter code with q_view doesn't work for me, but this does :

if (qa_get_state() === 'edit-'.qa_request_part(0)) {

}

Shady code, probably, but it's only for a small feature in the editor anyways.
+1 vote
by

I always use:

            if($this->template=='question' && strpos(qa_get_state(),'edit')!==false) {

for my plugins.

by
This won't actually work, as I explained in the answer. In this case you are not in the context of a theme/layer hierarchy so you can't use the 'template' variable
by
In a layer when e.g. overriding the head_script() it works.

But as I see now, the OP asked about get_field which is part of the editor module: http://www.question2answer.org/modules.php?module=editor

The question title should be changed to "How can an editor module ..."
by
yus you right, changed the title
...