Keyword

Unique Classes for K2 Extra Fields Value

More
7 years 8 months ago #157039 by JP
Hello,

I am using an array to customize category_item.php and item.php. The following array allows me to call the value of the extra field using the extra field ID and place it where I'd like it on the page:

<!-- Call to prepare extra fields -->
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>


This works great for display, but now I need to assign an icon to the label of the value. For example, if the value of an extra field is "Apple", then I need a unique class for it's label so that I can use CSS to display an apple icon. The same goes for when the output is Orange. In summary, I would like to display an image of an apple next to the word "Apple", so I need unique classes for each value option. How can I accomplish this?

Thank you!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 8 months ago #157052 by Krikor Boghossian
Replied by Krikor Boghossian on topic Unique Classes for K2 Extra Fields Value
Since $item->value and $extrafields[$item->id] hold the value of the field, you can check the value, with an if statement and depending on the result print the appropriate HTML code.

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

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

More
7 years 8 months ago #157060 by JP
Thanks, Krikor!

Unfortunately, I don't hand-code PHP. If you are able to, could you please provide the appropriate snippet to accomplish this?

Thank you!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 8 months ago #157073 by Krikor Boghossian
Replied by Krikor Boghossian on topic Unique Classes for K2 Extra Fields Value
I can provide a starting point but the code you sent does not actual print data.

Another approach would be to render each field individually.
This would be a sufficient starting point:
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L104-L109

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

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

More
7 years 8 months ago #157092 by JP
Thanks, Krikor.

The code I sent was sent as a reference for how I am displaying the values on other areas of the page. So I have that array and then I use the following to display an extra field value on the page:
<span class="alias"><?php echo $extrafields[8];?></span>

This works great when I don't need a unique class for each individual value option. For one section of the page, each value has an image icon associated with it, so I need to have 3 unique classes for 3 values for 1 extra field. I have been trying to use what you sent me and other threads to create a unique class for each value, but I keep coming up short. Currently, I have:
<span class="itemExtraFieldsValue <?php echo $extraField->alias; ?>"><?php echo $extraField->value; ?></span>
<span class="alias"><?php echo $extrafields[2];?></span>

This second span line works great, but the first one just produces a blank class: <span class="itemExtraFieldsValue "> </span>.
If I could get to something like the following, I would be able to assign an image to span.itemExtraFieldsValue optionA and then display the value using <span class="alias"><?php echo $extrafields[2];?></span>:
  • <span class="itemExtraFieldsValue optionA">Value A displays here</span>
  • <span class="itemExtraFieldsValue optionB">Value B displays here</span>
  • <span class="itemExtraFieldsValue optionC">Value C displays here</span>
If you could give more specifics on how to write out this line (or lines) of PHP to accomplish the unique class for the 3 extra field values, that would be much appreciated. I'm against a tight deadline and am coming up short with this, so any detailed and direct assistance is greatly appreciated!

Thanks!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 8 months ago - 7 years 8 months ago #157119 by Krikor Boghossian
Replied by Krikor Boghossian on topic Unique Classes for K2 Extra Fields Value
<?php
// Render a specific extrafield
if( isset( $this->item->extraFields->EXTRAFIELDALIASHERE->value ) && ( $this->item->extraFields->EXTRAFIELDALIASHERE->value !== '') ) 
{ ?>
	<span class="itemExtraFieldsValue OPTION_WHICH_BELONGS_TO_THE_ALIAS">
		<?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>
	</span>
<?php } ?>

Based on the field's alias, populate this snippet. Replace the alias and the class with the ones you want to use.
Copy/paste for as many fields you have.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 7 years 8 months ago by Krikor Boghossian.

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

More
7 years 8 months ago - 7 years 8 months ago #157147 by JP
Thanks, Krikor!

This is very close to what I need, so I did a little bit of manipulating what you sent to generate a unique class for each value:

<?php
// Render a specific extrafield
if( isset( $this->item->extraFields->EXTRAFIELDALIASHERE->value ) && ( $this->item->extraFields->EXTRAFIELDALIASHERE->value !== '') )
{ ?>
<span class="itemExtraFieldsValue <?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>">
<?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>
</span>
<span class="optionA"><?php echo $extrafields[2];?></span>
<?php } ?>

This allowed me to get span.itemExtraFieldsValue optionA, span.itemExtraFieldsValue optionB, and span.itemExtraFieldsValue optionC. Then I displayed the actual value next to that, and used CSS to display an icon instead of text for span.itemExtraFieldsValue optionA through optionC next to the actual output value.

Thank you so much for your help and quick responses! It's much appreciated!
Last edit: 7 years 8 months ago by JP. Reason: more descriptive word

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 8 months ago #157174 by Krikor Boghossian
Replied by Krikor Boghossian on topic Unique Classes for K2 Extra Fields Value
You simply need to replace the EXTRAFIELDALIASHERE part with the actual alias of each extrafield.

This way you can control which field is being shown, thus entering the HTML code you want.

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

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

More
7 years 8 months ago #157196 by JP
I understand, but the snippet you provided wasn't creating a unique span class for each value, it was creating a class for the field, but I needed specific control over each of the value options with unique classes for each option. By moving some of the PHP into the class, I was able to create a unique class:

<span class="itemExtraFieldsValue <?php echo $this->item->extraFields->EXTRAFIELDALIASHERE->value; ?>">

Thanks again!

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 8 months ago #157214 by Krikor Boghossian
Replied by Krikor Boghossian on topic Unique Classes for K2 Extra Fields Value
You 're welcome JP.

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