Keyword

Link in custom modul from database

  • Henrik Brogaard
  • Henrik Brogaard's Avatar Topic Author
  • Offline
  • New Member
More
2 weeks 4 days ago #171637 by Henrik Brogaard
Henrik Brogaard created the topic: Link in custom modul from database
I would like to create a custom module where an external link to a webshop must be made from one of my extra fields in a given article

For example, if it is a review of a new Pearl Jam album.
In one of my extra fields, the band is named Pearl Jam
There I would like to create a module that says
"Buy this or other albums from Pearl Jam here"
As links to  website.com/search?q=Pearl+jam

But how do I do that?
I know I have to finde the information from my extra fields in the database and put it in as a link
<a href=' website.com/search?q={$row }'>

I hope someone can help me.

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 weeks 4 days ago - 2 weeks 4 days ago #171640 by Fotis
Fotis replied the topic: Link in custom modul from database
If you add the info in each item's extra fields, you can simply call *specific* extra fields (and not the entire loop) by using the extra field alias value. More specifically:

=== Call specific extra fields in K2 templates ===
To directly output individual extra fields in your K2 overrides, do something like this (e.g. in item.php) to get the extra field name:
echo $this->item->extraFields->EXTRAFIELD_ALIAS_HERE->name;

To get the extra field value you would simply do this:
echo $this->item->extraFields->EXTRAFIELD_ALIAS_HERE->value;
echo $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:
echo $item->extraFields->EXTRAFIELD_ALIAS_HERE->value;
echo $item->extraFields->EXTRAFIELD_ALIAS_HERE->rawValue; // for date type only

This way you can call any URL wherever you want inside your item.php template override.

* I have added this as a tip in K2's docs here: getk2.org/documentation/tips-a-tricks/2667-display-single-extra-fields-anywhere-in-your-k2-content


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
Last edit: 2 weeks 4 days ago by Fotis.

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

  • Henrik Brogaard
  • Henrik Brogaard's Avatar Topic Author
  • Offline
  • New Member
More
2 weeks 1 day ago #171658 by Henrik Brogaard
Henrik Brogaard replied the topic: Link in custom modul from database
thank you very much
It works perfectly by overwriting the K2 template.

Unfortunately, it does not work in a custom module.
my code is:
{source}
<?php echo $item->extraFields->BandKunstner->value; ?>
<?php echo $item->extraFields->BandKunstner->rawValue; // for date type only ?>
{/source}

What am I doing wrong?

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

  • Henrik Brogaard
  • Henrik Brogaard's Avatar Topic Author
  • Offline
  • New Member
More
1 week 4 days ago #171678 by Henrik Brogaard
Henrik Brogaard replied the topic: Link in custom modul from database
I dont follow.
What do you Mean?

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
5 days 18 hours ago #171737 by Fotis
Fotis replied the topic: Link in custom modul from database
@Henrik

The code does not work in a custom module because it's out of context. "$this->item" refers to the current K2 item being displayed by the K2 component. That's why you can utilize this code in the item.php override.

If you want to use this code in a side module on the item view or elsewhere, you'll need to get the item ID from the request/URL and then perform a custom query, parse the results to load extra fields and then render the field you want. Which is probably overkill... However there are ready-made modules that can load item elements, so your best bet is to find one and override it with the code I gave you.


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

Powered by Kunena Forum