0

I'm building a page containing a channel form to allow users to edit their posts, and I thought I'd add an {if}...{/if} statement to prevent them editing other users' entries. The permissions on the channel are set correctly, but this was just to get around the error message on form submission. So I added this:

{exp:channel:form .... entry_id="{segment_3}"}
  {if author_id <> member_id}
    {redirect="home"}
  {/if}
{/exp:channel:form}

The issue then, was that the code redirected even when author_id = member_id. This seemed bizarre, so as a test, I changed the above code to read:

{exp:channel:form .... entry_id="{segment_3}"}
  {if author_id <> member_id}
    Not Equals
  {if:else}
    Equals
  {/if}
{/exp:channel:form}

and found that regardless of which entry I edit, the statement returned

Not Equals

Upon a bit more playing around, I found that if I add an {author_id} tag above the {if}...{/if} statement, it worked properly. It seems that unless I echo the {author_id} before the comparison, {author_id} is... blank?

Can someone explain this?

1

That sounds like expected behaviour. If you're using {author_id} in the context of a Channel Form then it is to submit and entry into ExpressionEngine. Therefore until you do make a submission the form has no author_id even though the entry does. It's a little different to the Channel Entries tag in that respect as that's just retrieving an entry.

By outputting {author_id} before the conditional in the same template you're effectively setting a value to be compared. You could get around this by simply setting the author_id as a hidden input above the conditional in a similar way as you're doing now, or you could pass it in as an embed variable.

Looking at what you're trying to achieve however, you could just look to include the author_only="yes" parameter to the Channel Form along with a return parameter.

{exp:channel:form channel="my_channel" entry_id="{segment_3}" author_only="yes" return="template_group/template/ENTRY_ID"}

    ...

{/exp:channel:form}

This would then ensure that only the author of the entry can edit it and would fall back to the system Message Pages if the user isn't authorised. You can then style or control those how you choose but that's another question in itself. :)

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.