XF 2.0 Return error message in function actionEdit

AndyB

Well-known member
In an add-on I'm creating for XF2 called 'Edit delete limit', I would like to be able to return an error message when the Edit link is clicked below a post and certain contions have been met. I have extended the actionEdit controller, but I'm not having any luck using the standard return error:

return $this->error(\XF::phrase('editdeletelimit_edit_time_limit_has_expired'));

Is there any way to send an error message when extending the actionEdit method?

There are no error messages, I click the Edit link and nothing happens.

Thank you.
 
Last edited:
Here is the code, it all works great except the return error.

PHP:
<?php

namespace Andy\EditDeleteLimit\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Post extends XFCP_Post
{	
	public function actionEdit(ParameterBag $params)
	{		
		// get parent		
		$parent = parent::actionEdit($params);

		// get options
		$options = \XF::options();	
		
		// get options from Admin CP -> Options -> Edit delete limit -> Usernames
		$usernames = $options->editDeleteLimitUsernames;

		// get options from Admin CP -> Options -> Edit delete limit -> Minutes
		$minutes = $options->editDeleteLimitMinutes;
		
		// get visitor
		$visitor = \XF::visitor();
		
		// get userId
		$userId = $visitor['user_id'];
		
		// check if in_array
		if (in_array($userId, $usernames))
		{
			// get post
			$post = $this->assertViewablePost($params->post_id);
			
			// get postId
			$postDate = $post->post_date;	

			// get dateLine
			$dateLine = time() - ($minutes * 60);

			// if past edit limit
			if ($postDate < $dateLine)
			{
				// return error
				return $this->error(\XF::phrase('editdeletelimit_edit_time_limit_has_expired'));
			}
		}
		
		// return parent
		return $parent;
	}
}
 
I think it's because data-xf-click attribute of the Edit link is quick-edit, not overlay, it seems the XF JavaScript library doesn't handle cases correctly where the response is an error. A XF developer can give you a more accurate reply but XF core JavaScript code may need a change.

On another note, your code runs the parent function before it checks for conditions, so it may be possible to actually edit the post by directly sending a POST request. Not easy for most users but technically possible.
 
Hi Pat,

Thank you for looking at this for me.

I was able to get the add-on to work by extending this instead:

PHP:
<?php

namespace Andy\EditDeleteLimit\XF\Entity;

class Post extends XFCP_Post
{
	public function canEdit(&$error = null)
	{
<snip>
 
Back
Top Bottom