how to refer to vars from query in hook frontend_fetchentry

Creating and modifying plugins.
Post Reply
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

how to refer to vars from query in hook frontend_fetchentry

Post by peggylon » Thu Feb 18, 2016 6:15 pm

Hi,

Unfortunately I'm not familiar with s9y plugins but from docs and examples I'm slowly getting along to create a simple question and answers-plugin. I'm aware of the existence of a plugin featuring a full blown discussion board. I'm looking for something slightly different, though.

I could succesfully create db tables, enter question for entry in appointed field in the backend as well as save that question to the database.
When fetching single entries featuring questions, I'd like to display those along with answers.
Something like:

Code: Select all

case 'frontend_display':
    			
    				if(in_array($eventData['id'], $this->editEntries)) {
    					$eventData['body'] .= $eventData['question'];
             $eventData['body'] .= $this->getAnswerForm();
             $eventData['body'] .= $eventData['answers'];

    					}
    				return true;
    				break;
The freetag plugin which is kind of my role-model uses a changed query to retreive tags while fetching entries. I adoptet my plugin accordingly, in order to fetch the entry's question. Right now answers are not yet part of the game.

Code: Select all

case 'frontend_fetchentries':
case 'frontend_fetchentry':
    					if(in_array($eventData['id'],$this->editEntries)) {
    							$questionID = $eventData['id'];
    					
    						$cond = $join = '';
    						$cond  .= "entryquestions.question AS question,\n";
    						
    						if (empty($eventData['addkey'])) {
    							$eventData['addkey'] = $cond;
    						} else {
    							$eventData['addkey'] .= $cond;
    						}
    						
    						$join = "LEFT JOIN {$serendipity['dbPrefix']}entryquestions AS entryquestions ON (e.id = entryquestions.entryid) ";
    						$cond = "entryid = $questionID";
    						
    				
    						if (empty($eventData['and'])) {
    							$eventData['and'] = " WHERE $cond ";
    						} else {
    							$eventData['and'] .= " AND $cond ";
    						}
    				
    						if (empty($eventData['joins'])) {
    							$eventData['joins'] = $join;
    						} else {
    							$eventData['joins'] .= $join;
    						}
    				
    						$this->displayQuestionId = $questionID;
    						$serendipity['plugin_vars']['displayQuestionId'] = $questionID;
    						@define('PLUGIN_VARS_DISPLAYQUESTION_ID', $questionID);
    					}
    					
    					return true;
    					break;
What I don't see, is how I can use the additional data (entry question) in the frontend_display hook.
The freetag plugin on the opposite works with hook entry_display instead, where an additional db query is run in order to get entries' tag data.

Hope, I'm on the right track and thank in advance for any plugin dev help. :-)

ciao, Peggy
----------
peggylon aka multikulinaria http://www.multikulinarisch.es

User avatar
onli
Regular
Posts: 2163
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by onli » Fri Feb 19, 2016 2:40 pm

I hope I miss nothing. But I think you're on the right track.

Code: Select all

$serendipity['plugin_vars']['displayQuestionId'] = $questionID;
This saves the ID in the global serendipity array. If PHP did not stop in the meantime – frontend_display is executed after frontend_fetchentry, but in the same request – then $serendipity will still contain that id. You'd just do:

Code: Select all

global $serendipity;   # if not done before
echo $serendipity['plugin_vars']['displayQuestionId'];
It could be nicer to instead manipulate $eventData or $addData of fetchentry, one of those is permanent and should end up in the $entry array that is given as $eventData to frontend_display. But I'd have to look it up whether anything of that is true.

peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by peggylon » Fri Feb 19, 2016 4:52 pm

Thanks onli,

the ID is not what I'm after. It's the question itself. As I understand it gets retrieved along-side entry data (since the query was changed that way). But I dunno how to grab/use that query's result.

Hope that helps to clarify the issue.

cheers
Peggy
----------
peggylon aka multikulinaria http://www.multikulinarisch.es

User avatar
onli
Regular
Posts: 2163
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by onli » Fri Feb 19, 2016 5:28 pm

Ah, okay. That's because you modify the sql statement only at first. That's not necessary.

Note: The freetag plugin is a monstrum. It does some interesting things, but don't just copy the way it does things without questioning them first. Just use it as a solution toolbox for specific things.

You could work just with frontend_display. $eventData contains the data of the entry (have a look with `print_r($eventData);`). Use that data to get your question id. Then use that id to get the question + the data you want to display. Assign the question to smarty and output that smarty object properly formatted in your template (entry.inc.tpl).
But I dunno how to grab/use that query's result.
Based on your example code, that should've filled $eventData['question'] in all following higher-level events where $eventData is the entry. Is the disconnect there, or at the level of giving things to the frontend, to echo them as html?

peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by peggylon » Fri Feb 19, 2016 6:13 pm

You could work just with frontend_display. $eventData contains the data of the entry (have a look with `print_r($eventData);`). Use that data to get your question id. Then use that id to get the question + the data you want to display. Assign the question to smarty and output that smarty object properly formatted in your template (entry.inc.tpl).
So you're suggesting to not editing the entry query in hook frontend_fetchentry, but implement a separate db query to fetch question and answers for a given entry?
Note: The freetag plugin is a monstrum. It does some interesting things, but don't just copy the way it does things without questioning them first. Just use it as a solution toolbox for specific things.
I know. Thats why I started with an empty script, gradually figuring out which hooks I need and what (not) to do there.
Is the disconnect there, or at the level of giving things to the frontend, to echo them as html


Right there. I tried outputting $eventData['question'] but it seems empty or not existing, even though there is a question related to that entry in the database. In preview mode the following snippet in hook frontend_display returns Test_.

Code: Select all

 $eventData['body'] .= 'Test_' . $eventData['question'];
Thanks so far!
Last edited by peggylon on Fri Feb 19, 2016 6:26 pm, edited 1 time in total.
----------
peggylon aka multikulinaria http://www.multikulinarisch.es

User avatar
onli
Regular
Posts: 2163
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by onli » Fri Feb 19, 2016 6:22 pm

peggylon wrote:So you're suggesting to not editing the entry query in hook frontend_fetchentry, but implement a separate db query to fetch question and answers for a given entry?
Yes. Potentially, a separate query can be slower, but it is easier to structure and the spared join can also help performance, depending on the data and the characteristics of the DBMS (at least in the past, mysql was said to be bad there).
Right there. I tried outputting $eventData['question'] but it seems empty or not existing
Have a look at print_r, it is really helpful in situations like this as it gives you the full array content.

peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by peggylon » Fri Feb 19, 2016 6:35 pm

Yes. Potentially, a separate query can be slower, but it is easier to structure and the spared join can also help performance, depending on the data and the characteristics of the DBMS (at least in the past, mysql was said to be bad there).
I'll check the $eventData content via print_r. But I think I might do as you said and skip the frontend_fetchentry hook.
----------
peggylon aka multikulinaria http://www.multikulinarisch.es

peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: how to refer to vars from query in hook frontend_fetchen

Post by peggylon » Mon Feb 22, 2016 4:43 pm

I checked $entryData. It doesn't contain the question part. I'll forget about that approach and use seperate db query instead. Thanks for your advice, Oli,

Here's the result of print_r($eventData):

Code: Select all

Array
(
    [id] => 1040
    [title] => Eierkuchen mit Vogelbeer-Karamell
    [timestamp] => 1455692400
    [body] => ...
    [comments] => xx
    [trackbacks] => 0
    [extended] => 
    [exflag] => 0
    [authorid] => 1
    [isdraft] => false
    [allow_comments] => true
    [last_modified] => 1456140125
    [moderate_comments] => false
    [author] => xxx
    [loginname] => xxx
    [email] => xxx
    [categories] => Array ...
    [properties] => Array
        (
            [meta_description] => ...
            [meta_keywords] => ...
            [ep_cache_body] => ...
            [ep_VGWort] => ...
            [freetag_tags] => Array ...
            [freetag_tagList] => dessert,ei,vogelbeere,wildobst
        )

    [freetag] => Array
        (
            [extended] => 1
            [tags] => Array
                (
                    [description] => Tags für diesen Artikel: 
                    [tags] => ...

                )

            [related] => Array
                (
                    [description] => Artikel mit ähnlichen Themen:
                    [entries] => Array ...     
                )

        )
----------
peggylon aka multikulinaria http://www.multikulinarisch.es

Post Reply