K2 Layout Category Item Extra Field

GK User
Fri Jan 23, 2015 12:28 pm
Hello, I am trying to return the value of an extra field on the category item view under the header of an item.

I keep getting a "foreach warning" for this code:

Code: Select all

                              <header>
                                        <?php if(isset($this->item->editLink)): ?>
                                        <a class="catItemEditLink modal" rel="{handler:'iframe',size:{x:990,y:550}}" href="<?php echo $this->item->editLink; ?>"> <?php echo JText::_('K2_EDIT_ITEM'); ?> </a>
                                        <?php endif; ?>
                                        <h2>

                  <!-- START: Call to prepare extra fields -->
                          <?php
                          //convertArray to use ids as key
                          $extrafields = array();
                          foreach($item->extra_fields as $extraFlds)
                          {
                          $extrafields[$extraFlds->id] = $extraFlds->value;
                          }
                          ?>
                                                  <?php if ($this->item->params->get('catItemTitleLinked')): ?>
                                                  <a href="<?php echo $this->item->link; ?>"><?php echo $this->item->title; ?></a></br>
                                                  <a href="<?php echo $this->item->link; ?>"><?php echo $extrafields[13];?></a>
                                                  <?php else: ?>
                                                  <?php echo $this->item->title; ?>
                                                  <?php endif; ?>
                                                  <?php if($this->item->params->get('catItemFeaturedNotice') && $this->item->featured): ?>
                                                  <sup><?php echo JText::_('K2_FEATURED'); ?></sup>
                                                  <?php endif; ?>
                                        </h2>
                              </header>


As for now it's in the header, but as soon as I got it working I will put it under as a description.

Thank you,
Regards
User avatar
Platinum Boarder

teitbite
Sun Jan 25, 2015 9:17 am
Hi

What warning it gives ? If it's "Wrong parameter" one than this will means that extrafields are not picked up from database in category layout and You will need to add such query on Your own or ask K2 support to tell You how to extend model of category view.
User avatar
Moderator

GK User
Mon Jan 26, 2015 6:02 am
Hi Teitbite,

the warning it gives .. Here it is:

Code: Select all
Warning: Invalid argument supplied for foreach() in /home2/guitar88/public_html/templates/gk_university/html/com_k2/templates/custom_view1/category_item.php on line 37


Line 37 is:

Code: Select all
foreach($item->extra_fields as $extraFlds)



Regards.
User avatar
Platinum Boarder

teitbite
Tue Jan 27, 2015 6:44 pm
Hi

It's hard to tell what could be the problem without making a dump and my access to your site is not working any more, so please make it for me and tell me what code was generated. Add this line before this line 37.

Code: Select all
print '<pre>'; var_dump( $item->extra_fields ); print '</pre>';
User avatar
Moderator

teitbite
Tue Jan 27, 2015 6:46 pm
Hi

Thinks a little longer about that I guess it does not matter what the value it presents. Just replace the code:

Code: Select all
                  <!-- START: Call to prepare extra fields -->
                          <?php
                          //convertArray to use ids as key
                          $extrafields = array();
                          foreach($item->extra_fields as $extraFlds)
                          {
                          $extrafields[$extraFlds->id] = $extraFlds->value;
                          }
                          ?>


with:

Code: Select all
<?php if( $item->extra_fields ): ?>
                  <!-- START: Call to prepare extra fields -->
                          <?php
                          //convertArray to use ids as key
                          $extrafields = array();
                          foreach($item->extra_fields as $extraFlds)
                          {
                          $extrafields[$extraFlds->id] = $extraFlds->value;
                          }
                          ?>
<?php endif; ?>
User avatar
Moderator

GK User
Wed Jan 28, 2015 2:07 am
I've tested the code, the warning is gone, but the extra_field_13 value is not returned.

I haven't revoked your FTP or backend access, so I will private msg you again the information just in case.

Thank you
User avatar
Platinum Boarder

GK User
Thu Jan 29, 2015 4:52 pm
Anything new on this Teitbite?

thank you
User avatar
Platinum Boarder

teitbite
Fri Jan 30, 2015 7:47 pm
Hi

I've adjusted this code a little bit:

Code: Select all
               <?php $extrafields = array(); ?>
               <?php if( is_array( $item->extra_fields ) ): ?>
                  <?php //convertArray to use ids as key
                     foreach($item->extra_fields as $extraFlds){
                        $extrafields[$extraFlds->id] = $extraFlds->value;
                     }
                  ?>
               <?php endif; ?>                                                 

               <?php if ($this->item->params->get('catItemTitleLinked')): ?>
                  <a href="<?php echo $this->item->link; ?>"><?php echo $this->item->title; ?></a></br>
                  <?php if($extrafields[13]): ?>
                     <a href="<?php echo $this->item->link; ?>"><?php echo $extrafields[13]; ?></a>
                  <?php endif; ?>
               <?php else: ?>
                  <?php echo $this->item->title; ?>
               <?php endif; ?>


this code basically tests if extra fields exists, becuase if not than there is no way of extrafields[13] to exists as well.


Also there is something strange on your server. I cannot make a dump. My code is being stripped out.
User avatar
Moderator

GK User
Mon Feb 02, 2015 4:40 am
Thank you Teitbite. I've just checked your code and verified that the extra field 13 is in use. I've also tried with other extra fields that I'm sure have values within the items but still nothing on the category_item layout.

Maybe I am doing something wrong?

These are my extra field, they are used for the template Suivi_K2:
http://www.quebecguitare.ca/administrat ... xtrafields

This is one of the category with the template Suivi_K2 assigned:
http://www.quebecguitare.ca/suivi/guyla ... -m-quebec/

This is one K2 Item under that category with the extra_field_13 that contains a value:

http://www.quebecguitare.ca/suivi/guyla ... e-mathis-m

--The ExtraField13 is named "Résumé" and within this K2 Item contains the following value that we should see in the header of the category_item layout: "Bonjour Lucie, voici le Suivi pour le cours de cette semaine. On a eu un très bon cours.

On a regardé surtout les pièces pour le concert."


As for the "I cannot make a dump. My code is being stripped out." Do you want me to inquire to Hostgator tech support for that?

Thank you,
Regards
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 10:52 am
Hi

Whatever I do it makes no effect. I do not know if I'm changing a wrong file or server cache is not showing changes. Please ask hosting to disable any caching they may have. It's not possible to work right now. I've put a piece of code into category layout file of /suivi_k2, so it may look broken when cache refreshes, but now it's the only way I can check if extrafields are used at all.

PS: I've found out that extrafields were disabled in category settings. I've enabled it but no change and I do not know again if it's because of cache of simply not the issue.
User avatar
Moderator

GK User
Tue Feb 03, 2015 11:03 am
I am extremely sorry, I forgot to mention that the site is under CDN. But php files are not under CDN.

Under plugin on the backend you need to disable Système - NoNumber CDN pour Joomla!, if that helps.

Otherwise, purging cache in Joomla?

Regards
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 11:04 am
Hi

Definitely cache related. "NULL" appeared when I made a dump of extrafields picked up from database after 10min.
User avatar
Moderator

GK User
Tue Feb 03, 2015 11:05 am
I see it, what should I do?
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 11:24 am
I have disabled extra field in category item view:

I see that this category has still has extra field in category item view: http://www.quebecguitare.ca/suivi/thoma ... -montreal/

But all the others I've checked dont; http://www.quebecguitare.ca/suivi

I am in queue for technical support with Hostgator. I am gonna ask them about your <pre> dump and the cache, to the best of my knowledge.

Regards
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 11:28 am
Hi

<pre> works, is just delayed, so no need to involve hostgator at the moment. I'll check the $item and see what's wrong with extrafields.
User avatar
Moderator

GK User
Tue Feb 03, 2015 11:29 am
OK I'll hang up my queue with HG tech support. Thanks
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 11:32 am
So all I want is the extra_field_13 value in smaller font under the title of the category item layout. It contains a brief description of the item.

Thank you
Regards
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 11:33 am
Hi

I think I see the problem now. A code for extrafields was most probably copied from item layout page and variable names are wrong. I've corrected it and in 10 - 15min we can check if this worked.
User avatar
Moderator

GK User
Tue Feb 03, 2015 11:36 am
OK great! I'll wait..

I copied that code from several sites. I don't know what I am doing, I must have done a mistake changing it not knowing there was a delayed refresh.

Thank you very much Teitbite, you are being must useful and patient.

Best Regards
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 12:17 pm
OK the dump worked! But now all the fields are there before the header.

I just need extra_fields_13 after the header. It's not there.
Thank you,
Regards
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 12:44 pm
I've put in comment your dump <pre> code because this is production site.

Here is the result for the first item of this category item layout:

Code: Select all

string(1006) "[{"id":"9","value":"\/images\/avatar\/cllegareThumbs_16x9.jpg"},{"id":"10","value":"2014-10-18"},{"id":"13","value":"
Bonjour Yanick, voici un r\u00e9sum\u00e9 du cours du 18 octobre 2014:<\/p>"},{"id":"12","value":"
motif 1 ascendant et descendant (en triades):
- tonalit\u00e9 de C: position de C, D et r\u00e9viser position de E
- tonalit\u00e9 de G: Commencer avec postions de G et D<\/p>"},{"id":"14","value":"
pratiquer les deux licks de bends tenus:
-lick 1
-lick 2<\/p>"},{"id":"15","value":"
pratiquer les mesures 1 \u00e0 4 de la deuxi\u00e8me \u00e9tude de chicken picking<\/p>"},{"id":"16","value":"Rien pour cette semaine..."},{"id":"17","value":"Rien pour cette semaine..."},{"id":"18","value":["","","new"]},{"id":"19","value":"1"},{"id":"20","value":"
Voil\u00e0 donc pour le cours de cette semaine. On se revoit la semaine prochaine \u00e0 la m\u00eame heure. Bonne semaine! Cl\u00e9ment Langlois-L\u00e9gar\u00e9,  Professeur  Qu\u00e9becGuitare.ca<\/p>"}]"



Source: http://www.quebecguitare.ca/suivi/cleme ... -montreal/

Regards
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 1:00 pm
Hi

I know it worked, like I've said it needed some time to show. And it revealed the problem. I think when extra fields are disabled in category settings than they are not being processed. Dump show one big string and not an array we were expecting. I've done some changes. Keep extra fields enabled. I'm gonna hide it in code instead.

See You in couple of minutes :)
User avatar
Moderator

GK User
Tue Feb 03, 2015 1:02 pm
Yayyy! Brilliant Teitbite!

Regards
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 1:05 pm
There is a white screen with this... we just wait?


Code: Select all
Suivi en Ligne Cours de Guitare Privé Québec Guylaine Beaulieu


Parse error: syntax error, unexpected end of file, expecting ',' or ';' in /home2/guitar88/public_html/_quebecguitareca/templates/gk_john_s/html/com_k2/templates/suivi_k2/category_item.php on line 177

Warning: mysqli_close(): Couldn't fetch mysqli in /home2/guitar88/public_html/_quebecguitareca/libraries/joomla/database/driver/mysqli.php on line 210

Warning: mysqli_close(): Couldn't fetch mysqli in /home2/guitar88/public_html/_quebecguitareca/libraries/joomla/database/driver/mysqli.php on line 210

Warning: mysqli_close(): Couldn't fetch mysqli in /home2/guitar88/public_html/_quebecguitareca/libraries/joomla/database/driver/mysqli.php on line 210

Warning: mysqli_close(): Couldn't fetch mysqli in /home2/guitar88/public_html/_quebecguitareca/libraries/joomla/database/driver/mysqli.php on line 210

Warning: mysqli_close(): Couldn't fetch mysqli in /home2/guitar88/public_html/_quebecguitareca/libraries/joomla/database/driver/mysqli.php on line 210

User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 1:15 pm
Hi

Please ask hosting to disable this cache. It's not possible to work like that. I've made a small typo and had to wait to see it than wait since I saw it fixed. It takes too much time this way.
User avatar
Moderator

GK User
Tue Feb 03, 2015 1:21 pm
OK I'm back in the HG tech support queue. 20 min left.

Will update ..
User avatar
Platinum Boarder

GK User
Tue Feb 03, 2015 1:50 pm
Hosting Caching has been disabled.
User avatar
Platinum Boarder

teitbite
Tue Feb 03, 2015 2:34 pm
Hi

It's still the same :( I'll try to look how to disable it.
User avatar
Moderator

teitbite
Wed Feb 04, 2015 1:40 pm
Hi

I think extra fields are not provided for category layout. What we see are informations about the field not the text You put in. Can You please one more time ask hosting to disable this cache. It's impossible to figure this problem out when I need to run back and forth.

Code: Select all
object(stdClass)#974 (8) {
    ["id"]=>
    string(2) "13"
    ["name"]=>
    string(8) "Résumé"
    ["value"]=>
    NULL
    ["type"]=>
    string(8) "textarea"
    ["group"]=>
    string(1) "2"
    ["published"]=>
    string(1) "1"
    ["ordering"]=>
    string(1) "3"
    ["alias"]=>
    string(11) "coursresume"
  }
  [3]=>
User avatar
Moderator

GK User
Wed Feb 04, 2015 1:45 pm
Hostgator said they don't have server caching. To disable cache they said you either clear the joomla cache or you rename the folder. What do you think of that?

I just disabled gkCache and CDN plugin.

Is that enough you think?

Regards
User avatar
Platinum Boarder

GK User
Thu Feb 05, 2015 4:57 am
Hi Teitbite, thank you so much for you patience.

I did more research and went back into the history of a K2 Extra Field issue I had with technical support for the Extension Joocial that has an Autotweet K2 Plugin.

I had this plugin build to return extra field with the following expression [extra_field_13].
That plugin worked returning the value of the extra field.

That leads me to think this might be part of the answer. There might be a piece of code that interferes, a conflict or we are simply not looking at the right place.

Nevertheless, I had asked them a while back to tell me where the code is located in the plugin php file to specially retrieve any extra field.

So I hope this will be usefull for you. We could also use their variable to save time??

Joocial Support:
You can find the extra field processing in file: plugins/system/autotweetk2/autotweetk2.php, line 375


I've looked at the file and I think it's more around line 399

Code: Select all
// Extra Fields
      $pattern = '/\[extra_field_([0-9]+)\]/i';

      if ( (preg_match_all($pattern, $message, $matches)) && (isset($article->extra_fields)) )
      {
         require_once JPATH_ADMINISTRATOR . '/components/com_k2/models/extrafield.php';
         $extraFieldModel = K2Model::getInstance('ExtraField', 'K2Model');

         $keys = $matches[1];
         $article_extra_fields = json_decode($article->extra_fields);
         $extra_fields = array();

         foreach ($article_extra_fields as $field)
         {
            $extra_fields[$field->id] = $field->value;
         }

         foreach ($keys as $key)
         {
            $value = $extraFieldModel->getSearchValue($key, $extra_fields[$key]);
            $message = str_replace("[extra_field_{$key}]", $value, $message);
         }
      }



In attachment.

I hope that helps.
Regards

EDIT: From what I understand, it looks like a conflict to me. The variable we're already manipulated by that plugin. I think the value would be rather inside another variable, so we we're maybe off a bit.
User avatar
Platinum Boarder

teitbite
Fri Feb 06, 2015 1:43 pm
Hi

I've tried with this code, but it has the same problem I was talking about before. Extra fields are not picked from database for this. I've checked dump for the whole item and the only place it's used is in search fields. Here is an image with the content of extra fields when the regular K2 display is used. As You see only 3 elements are there and all the rest is presented in dump as NULL.

This must be codded somewhere deeper in core K2 functions. I think K2 developers will be more suitable here to help.
User avatar
Moderator

GK User
Tue Feb 10, 2015 7:04 am
Thank you Teitbite. I will open a thread on this on the K2 forum.
I will let you know if it gets solved.

Regards.
User avatar
Platinum Boarder

teitbite
Wed Feb 11, 2015 11:59 am
Hi

Thank You. I'm curious how to use extra fields in K2 categories as well. But the most interesting is why it shows some of the fields but not all.
User avatar
Moderator

GK User
Wed Feb 11, 2015 12:06 pm
Hi Teitbite, I am happy to say that I had a reply. Would that help:

Hello,

The category item's template required $this->item. eg:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>

You can also try to render a specific extrafield like this:
$this->item->extraFields->EXTRAFIELDALIASHERE->name
$this->item->extraFields->EXTRAFIELDALIASHERE->value



I thought we tried this already, aka with or without ->item... no?

Regards

Ref.: http://www.joomlaworks.net/forum/k2-en/ ... value-null
User avatar
Platinum Boarder

teitbite
Thu Feb 12, 2015 9:08 pm
Hi

Yes we have tried that but it was not working ;/ It lists extrafields but from unknown reasons the value for extrafield with id 13 is NULL. But the other method is something new. I do not recall this values from a dump, but K2 developers must know better. It's their code :)
User avatar
Moderator


cron