0

I've been working with Joomla's custom fields in an article override, but I'm not having any luck adding markup ONLY when a field is used. I have very limited knowledge of PHP.

Here's my statement:

<?php if(isset($this->item->jcfields[x]) === true  && empty($this->item->jcfields[x]) === false): ?>
    <div class="name"><?php echo $this->item->jcfields[x]->value; ?></div>
<?php endif; ?>

In my limited understanding this would seem to check if the field has content and is not empty. If true, display the second line that wraps the field value in a div. That works.

Conversely, if the field is empty, the second line should be skipped, which would not create the wrapping div.

In reality, the div is created either way.

What am I missing?

mccolley
  • 1
  • 1
  • For your information, the combination of `isset()` and `!empty()` is a very commonly used anti-pattern. Please read: https://stackoverflow.com/a/4559976/2943403 If you are doing Joomla work, please join [joomla.se] Stack Exchange and ask your Joomla questions there to reach an audience with an intimate understanding of the CMS. – mickmackusa May 03 '21 at 22:52

1 Answers1

0

I went a different way since Joomla fields didn't register the output as empty with the above code.

Instead, I retrieved the value of the field and searched the text:

    <?php 
        $word = "https";
        $mystring = ($this->item->jcfields[x]->value);
        if(strpos($mystring, $word) == false){echo "";
            } else{
        echo "<div class='facebook-link'>" . $mystring . "</div>";
        }
    ?>

Combined with this CSS:

.facebook-link a  {font-size: 25px; display: inline-block; text-indent: -999em; text-decoration:none;}
.facebook-link a:after {font-family: "FontAwesome"; display: block; text-indent: 0;content: "\f09a";}

The PHP code was added to an article override and wrapped in an outer wrapper that was designated as a css-grid with the grid-template-columns set to repeat{8, 40px).

The end result is a row of linked icons for whatever social media accounts the page user defines.

mccolley
  • 1
  • 1