Keyword

K2 Extra Fields Image Tag Src Extract

  • Design
  • Design's Avatar Topic Author
  • Offline
  • New Member
More
8 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
8 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
8 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
8 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
8 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
8 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
8 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
8 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
8 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
8 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.


Powered by Kunena Forum