Display K2 Extra Field Of Current Item In Module Position

  • Comments:View comments
Rate this item
(0 votes)

Learn how to display K2 extra field of current item in a module position.

Follow these quick and easy steps..

If you don't want to do the customizations yourself, download the customized module and use it out of the box

Before download...

Continue... ×

1- Download and install the module BNR Content for K2.

2- Edit file: modules/mod_k2bnr_content/mod_k2bnr_content.xml, insert this at line line no: 103

<field name="bnrItemExtraFieldAlias" type="text" size="" default="" label="Extr Field Alias" description="The alias of a specific extra field" />

It should go directly after this code piece:

<field name="bnrItemExtraFields" type="radio" default="1"   label="MOD_K2BNR_CONTENT_EXTRA_FIELDS_LABEL" description="MOD_K2BNR_CONTENT_EXTRA_FIELDS_DESC">
				<option value="0">Hide</option>
				<option value="1">Show</option>
			</field>

This will add a new parameter to specify the extra field to display in this module instance, the extra field will be specified by alias.

3- Edit file: modules/mod_k2bnr_content/tmpl/default.php, find this code piece:

<b><?php echo JText::_('Additional Info'); ?></b>
              <ul class="moduleItemExtraFields">
                <?php foreach ($item->extra_fields as $extraField): ?>
                                        <li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                                                <span class="moduleItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>
                                                <span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                                                <br class="clr" />		
                                        </li>
                <?php endforeach; ?>
              </ul>

 Replace it with this code piece:

<?php if($fieldAlias = $params->get('bnrItemExtraFieldAlias')): ?>
                <?php echo $item->extraFields->{$fieldAlias}->value;?>            
          <?php else: ?>
              <b><?php echo JText::_('Additional Info'); ?></b>
              <ul class="moduleItemExtraFields">
                <?php foreach ($item->extra_fields as $extraField): ?>
                                        <li class="type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                                                <span class="moduleItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>
                                                <span class="moduleItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                                                <br class="clr" />		
                                        </li>
                <?php endforeach; ?>
              </ul>
          <?php endif;?>

This will modify the module to display the extra field if a its alias is entered in the new parameter, otherwise it will display all extra fields as normal.

3- Now you can go and configure the module at the backend, set all parameters to Hide except for Extra fields, set it to Show and enter the field alias you want to display in the new parameter, it will look like this screenshot display k2 extra field in module position

If you have any questions or suggestions, feel free to comment on this post, or contact me directly via email or social media (on the above right corner of this page).

Read 1618 times