Keyword

Display Custom Field conditionally on menu id

More
2 months 2 days ago - 2 months 2 days ago #169077 by maz
maz created the topic: Display Custom Field conditionally on menu id
I am trying to place individual fields onto a template. My layout has a number of menu links to the SAME K2 page. I want different fields to show on the page and vary for each different menu link. corresponding to each separate joomla menu id.

a) I want to show these fields {field 24} {field 25}{field 26} on article linked to menu item id 1
b) I want to show these fields {field 20} {field 21}{field 22} on article linked to menu item id 2 (same article)
b) I want to show these fields {field 17} {field 18}{field 19} on article linked to menu item id 3 (same article)

Its the SAME article with multiple menu links. Just the menu item ID will change. for the different links.
The only example I have is for category shown below.

// Check if the item belongs to a certain category, category->id will work as well
if($this->item->category->name == 'Category Name')
{
then show fields 24,25,26
}

I need an IF statement to check the menu item id for the page rather than the category, and for example, IF this item id is 1 then it shows the fields 24,24,26..
Last Edit: 2 months 2 days ago by maz.

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Away
  • Administrator
  • JoomlaWorks Support Team
More
2 months 2 days ago #169080 by Fotis
Fotis replied the topic: Display Custom Field conditionally on menu id
First off, you need to retrieve individual extra fields.

=== Call specific extra fields in K2 templates ===
So how would you directly output individual extra fields in your K2 overrides? Simple. Just do something like this (e.g. in item.php) to get the extra field name:

$this->item->extraFields->EXTRAFIELD_ALIAS_HERE->name

To get the extra field value you would simply do this:

$this->item->extraFields->EXTRAFIELD_ALIAS_HERE->value
$this->item->extraFields->EXTRAFIELD_ALIAS_HERE->rawValue (for date type only)

Simply replace EXTRAFIELD_ALIAS_HERE with the actual alias of the extra field you wish to output.

In modules, use:

$item->extraFields->EXTRAFIELD_ALIAS_HERE->value
$item->extraFields->EXTRAFIELD_ALIAS_HERE->rawValue (for date type only)

AFTER you do the above, you need to place them in "if" statements that match your menu item ids.

So simply do this:
<?php

$itemid = JRequest::getInt('Itemid'); // Grabs the current menu item ID

if ($itemid == 1){
    // show extra fields 24, 25, 26
}

if ($itemid == 2){
    // show extra fields 20, 21, 22
}

if ($itemid == 3){
    // show extra fields 17,18, 19
}

I hope it's clear for you.

Easy peasy, only with K2 of course :)


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.

More
2 months 1 day ago - 2 months 1 day ago #169091 by maz
maz replied the topic: Display Custom Field conditionally on menu id
Thank you so much for such a clear explanation. That solved a massive issue for me. I originally had many thousands of page 'combinations' which you have helped me to instantly reduce to a few hundred. However, to get these to a handful, there are still some related issues I need help with.

1. Joomla uses {loadmodule positionX} to load a module directly (at positionX in this example) onto a joomla page. In need to do the same but onto K2 pages as per your guide above controlled by the itemid. For example:

if ($itemid == 1){
// show {loadmodule positionX}
}
I just need the exact code to replace the "// show {loadmodule positionX}" part, so that I can vary this to load different modules.

UPDATE
.... I am now thinking... is it just a simple matter of interlacing php with html? In other words, putting php tags around the php code and then adding html and then closing it with php again?? for example

<opening php tag> if ($itemid == 1){ <closingPHPtag>
<HTMLCODE> // show {loadmodule positionX} <CLOSING HTML CODE>
<opening php tag>}<closingPHPtag>

2. Similarly, I also want to run some third party plug-ins. These are basically graphs and pie chart plugins. Again as an example

if ($itemid == 1){
// run {plugin y}
}
I just need the exact code to replace the "// run {plugin y}" part, so that I can vary this for different joomla plugins.

3. And a very quick example of the use of ELSE based on field values. For example, field is colours.
so, IF field colour == red
show field 3 (or show a piece of text or load 'module positionX' for example)

Thank you
Last Edit: 2 months 1 day ago by maz.

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Away
  • Administrator
  • JoomlaWorks Support Team
More
2 months 23 hours ago #169092 by Fotis
Fotis replied the topic: Display Custom Field conditionally on menu id
1) It may be better to just trigger the code to render a module position within your K2 views. Use something like:
// Include a module position anywhere
<?php if(JModuleHelper::getModules('SOME_MODULE_POSITION_HERE')): ?>
<?php foreach (JModuleHelper::getModules('SOME_MODULE_POSITION_HERE') as $module): ?>
<div class="someClass">
	<?php echo JModuleHelper::renderModule($module, array('style' => '')); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>

If the above does not work in Joomla 3.x, you may need to add this before:
<?php

jimport( 'joomla.application.module.helper' );

2. You can't control that because depending on the plugin type, K2 already renders various plugin groups on different places. If the plugins are developed by you, add rendering conditions. If they are not and happen to be "system" type plugins, then you can just add the {plugin}whatever{/plugin} tags in your HTML and they will be rendered properly. Alternatively, you can create extra fields and add the plugin tags in a text-type field. K2 will trigger any content plugins that match the tag (perhaps this is a better option).

3. Use "$item->extraFields->EXTRAFIELD_ALIAS_HERE->value", e.g.
<?php

if ($item->extraFields->color->value == "red") {
    // do this if the color is "red"
}


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.

Moderators: william white