简体   繁体   中英

Joomla template override PHP initializing variable

I'm trying to create an override for my template to customize the way an extension should display it's fields in the article.

The extension I use is DPfields and I'm using this reference guide from the developer: https://joomla.digital-peak.com/documentation/162-dpfields/2750-rendering-fields

Expecially I'm referencing to the paragraph: Accessing the fields in the layout

I've created a new php file started from the default.php file for article view and inside this new file (newfile.php) I'm trying to display a gallery field type from the component DPField.

I've succesfully inserted this code in the newfile.php:

<?php
    foreach ($this->item->dpfields as $field) {
        $gallery = (($field->type)=='gallery');
        if (!empty($gallery)) {
            echo '<div class="galleryfield">' .$field->value. '</div>'; 
        }
    }
?>

so it correctly shows in the output the gallery.

My question is: how could I improve that code? Is there a better way to let it work instead of using a foreach?

Thanks in advance.

You can try to filter the array. It'll not change the amount of code you need I guess.

function filterForGalleryType($field) {
  return ($field->type) == 'gallery';
}

$galleryFields = array_filter($this->item->dpfields, "filterForGalleryType");

foreach ($galleryFields as $galleryfield) {
  echo '<div class="galleryfield">' .$galleryfield->value. '</div>'; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM