Keyword

K2 Extra Fields Image Tag Src Extract

  • Design
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #147978 by Design
Hello! I'm currently struggling in a way that I could use the K2 Image Extra Field as a link that I could myself implement in my site. I need some sort of PHP code that can extract only this part, taking notice I am already implementing the K2 fields into the PHP separately using the:
//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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #147991 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • Design
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #148010 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Sorry, but this doesn't look like what I'm talking about. This seems like a way of using K2 Extra Fields Image as the Item Image.
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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148018 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
I already sent you the code. Look at the highlighted section.
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
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #148022 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
So you're saying that if I use this:
<?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
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #148035 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Hey! I tried combining many of these techniques, and it actually was working, but I stumbled on something really weird.

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
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #148037 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Nevermind! Finally managed to do it!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148156 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
preg_replace requires a wildcard character to work correctly in this case.
$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
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago #148158 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract
Thanks for the help Krikor, but as I said before, I managed to solve it combining some functions I found around!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #148160 by Krikor Boghossian
Replied by Krikor Boghossian on topic K2 Extra Fields Image Tag Src Extract
Can you post your solution for reference?

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • Design
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago - 9 years 6 months ago #148163 by Design
Replied by Design on topic K2 Extra Fields Image Tag Src Extract

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>
Last edit: 9 years 6 months ago by Design.

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

More
9 years 6 months ago #148229 by Lefteris
Replied by Lefteris on topic K2 Extra Fields Image Tag Src Extract
Thanks for sharing this.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
6 years 4 months ago #170491 by Michael Rosa
Replied by Michael Rosa on topic K2 Extra Fields Image Tag Src Extract
If your Extra field image name is something other then Image like in my case it's calle List iamge the above regex doesn't work.

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.

More
6 years 4 months ago #170501 by JoomlaWorks
Replied by JoomlaWorks on topic K2 Extra Fields Image Tag Src Extract
I think we can add a "$this->item->extraFields->EXTRAFIELDALIASHERE->rawValue" output to only grab the image path. We already have this option for dates either way.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
6 years 1 month ago - 6 years 1 month ago #171562 by forty
Replied by forty on topic K2 Extra Fields Image Tag Src Extract
Fotis Can you give us all code for that.
I want to use it to change background of div from extrafield.
Thanks
Last edit: 6 years 1 month ago by forty.

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

More
6 years 1 month ago #171571 by JoomlaWorks
Replied by JoomlaWorks on topic K2 Extra Fields Image Tag Src Extract
You need to be more specific.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
6 years 3 weeks ago #171654 by forty
Replied by forty on topic K2 Extra Fields Image Tag Src Extract
Hi, i done it. Thanks!

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

More
6 years 1 week ago #171724 by JoomlaWorks
Replied by JoomlaWorks on topic K2 Extra Fields Image Tag Src Extract
Great!

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum