0

I am using the following code to check to see if my repeatable custom field's state is published/unpublished. If the state is set to 0, the code in between should be hidden, but instead an error is thrown.

Error: Notice: Trying to get property 'state' of non-object.

The "state" object is outputted as public 'state' => string '1' (length=1) when I use $programs = $fields['programs']; var_dump($programs);. I tried changing 0 to '0', but this does not work either.

<?php if($fields['programs']->state != 0): ?>
    <!-- Code is displayed here. -->
<?php endif; ?>

This code does work to check to see if a text field contains a value, but I am needing to ensure that the repeatable field is published before rendering the HTML and output.

<?php if($fields['article-description']->value != ''): ?>
    <!-- Code is displayed here. -->
<?php endif; ?>

Update

The custom fields code is being used in the Article default.php file. I have included how that code is being brought into the file below.

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers');
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$context = 'com_content.article';
$article = $this->item;
$fields = FieldsHelper::getFields($context, $article, true);

foreach($fields as $field) {
    $fields[$field->name] = $field;
}

When I unpublish the "programs" repeatable custom field, then do a var_dump of the $programs variable, Joomla displays this error: Notice: Undefined index: programs.

  • I have added a bit more info in my answer. – FFrewin Feb 24 at 19:39
1

It's unknown what exactly is your $fields variable, what values it contains (how it has been produced), where you use this code and generally your question lacks context.

However, note that when a field is unpublished - then it's not included in the $jcfields array of the article object, for which the field has been assigned as a custom one.

Now, given the conditions of your question, we may assume that you are in the context of an article object (could be a content plugin) and there it seems that your $fields array is the array that holds the CustomFields of your article. So, there, when you unpublish your "programs" custom field, your $fields array stops from containing it.

In addition, the PHP Notice you get is trying to say that: You ask an object property from something that isn't an object. Have you done var_dump($fields['programs']) - most likely that would be a null or simply var_dump($fields) when your programs field is unpublished? What do you get?

Published state works as expected. When you unpublish a field, the field will not be part of your content item. It won't be anywhere in your content, except sitting and waiting in the fields component.

====

I see you have updated your question... and this is what I had assumed: You are in the article context. My answer covers that. Var_dumping the $fields['programs'] produces "Undefined Index" notice because when the programs field is unpublished it doesn't exist in the article's fields array.

Finally, note that the article's custom fields are included already in the article object:

$this->item->jcfields

So, you don't need to do all that you do on top to get the custom fields.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.