- Posts: 13
COMMUNITY FORUM
K2 Extra Fields Image Tag Src Extract
- Design
-
Topic Author
- Offline
- New Member
//convertArray Extra Fields to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
//convertArray Extra Fields to use labels as key
$extrafieldslabels = array();
foreach($this->item->extra_fields as $itemlabel)
{
$extrafieldslabels[$itemlabel->id] = $itemlabel->name;
}
I found a article by google that shows this:
<?php
$var = $this->item->extraFields->Image->value ;
$var = preg_replace('/<img src="/',"",$var);
$var = preg_replace('/" alt="Image" \>/'," ",$var);
echo '<a href="' . $var . "TextOfLink" . "</a>" ;
?>
However it didn't work for me. I think it only works for the For loop, as I have seen by comments.
Does anyone have any idea how I can do this?
OBS: I need this because I need it to work something like this:
<div style="background-image: url('<?php echo $extrafields[28]; ?>.png')";>
Any help?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L76-L80
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
And I'm not really a PHP developer. I try to work my way around many times by testing millions of possibilities until it works, but I would really appreciate a more point specific answer.
So, its more of a matter if you know any way I could remove everything but the 'src="' value of the K2 Extra Fields Image in the item.php?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Replace the item.php's image block with and change the alias to your extrafield's alias.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
<?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>
The value of the image will come out as a pure string of the source, and not an IMG tag?
If so, I'll test it out later.
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
I started the item with this:
$icon = $this->item->extraFields->$extrafieldsalias[28]->value ;
$icon = preg_replace(array('/<img src="/', '/" alt="Icon" />/'),"",$icon);
I declared $icon and use $extrafieldsalias[28] to get the alias of the the Extra Field I wanted.
When I used preg_replace on the '/<img src="/', it worked and the actual <img src=" was removed from the $icon string as I desired, but the second part: '/" alt="Icon" />/' did not work as I expected. When I add this part, it actually erases the entire content and nothing shows up.
Do you have any idea why that is? I'm almost certain that it has nothing to do with the PHP function being wrongly written as it has been working and I got a few results. Well... Anything I could work around or any testing?
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
$toreplace = array(
'<img src="(.*?)" alt="Icon" />') =>'$1'
);
or getk2.org/documentation/tips-a-tricks/1129-extra-field-image-type-as-link which you might have already seen.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Design
-
Topic Author
- Offline
- New Member
- Posts: 13
Krikor wrote: Can you post your solution for reference?
Sure.
I combined all of this in the php header of item.php.
//convertArray Extra Fields to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
And this part is for actually stripping the IMG tag of the Image field I wanted, which had ID 28 (Still in header):
$icon = $extrafields[28];
$icon = preg_replace('/<img src="/',"",$icon);
$icon = substr($icon, 0, strpos($icon, '"'));
Later on, just declare the new variable $icon:
<div class="icon" style="background-image: url('<?php echo $icon; ?>')"></div>
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Moderator
- Posts: 8743
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Michael Rosa
-
- Offline
- New Member
- Posts: 5
This should work better, at least for me it does.
$var = $this->item->extraFields->EXTRAFIELDALIASHERE->value ;
$var = preg_replace('/<img src="(.*?)".*?\/>/', '$1', $var);
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- forty
-
- Offline
- New Member
- Posts: 2
I want to use it to change background of div from extrafield.
Thanks
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- forty
-
- Offline
- New Member
- Posts: 2
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.