1

I have a site where users can add entries of an entry type/section called Listings.

I would like to limit the number of Listings to a maximum of three per user.

In another CMS I did this by writing a plugin that got triggered when a new Entry was created. This plugin determined the number of existing Entries for a given user and if the maximum was reached, the plugin cancelled the creation of a new entry and instead showed a message that the user had reached their maximum limit of entries.

Not to say that this is the best way to do it, or do it in Craft, but I am new to Craft CMS and have been looking for a hook or event that is triggered when a new entry is created, but I don't seem to be able to find anything like that.

I have also done a fair bit of searching on this topic and cant seem to find any Stack questions or posts on the net....

Any suggestions much appreciated.....

1

You might want to take a look at this

Your condition will be

$author = Craft::$app->getUsers()->getIdentity();
$numberOfEntriesByUser = Entry::find()->sectionId($entry->getSection()->id)->authorId($author->id)->count();
if($numberOfEntriesByUser){
    $event->isValid = false 
} 

You should make sure the author is not an admin or Craft::$app->getRequest()->getIsCpRequest() is false otherwise you won't be able to save new entries as well.

Keep in mind to check for a current user as well. There won't be a user for cronjobs / other public post request

  • Hi Robin, Thanks once again for your help. I think the plugin sample code is just what I need - I will try it out. I appreciate you taking the time to provide that as a sample. However, the EVENT_BEFORE_SAVE event doesnt quite suit my needs because I think it would run once the user clicks Save, after having taken the time to fill in a whole listing. It would take a user some time to enter all the info, and then this would not be saved, which the user would not like. – ironfish May 18 '18 at 15:35
  • I need something that would alllow me to check after a user Clicks New entry but before the user sees the fields to fill in a listing, something like EVENT ON CREATE or EVENT ON NEW ENTRY but I dont see anything like that. Please let me know if you have some ideas about that.... – ironfish May 18 '18 at 15:35
  • You could eventually try to use the afterSaveElement event to check if the user already has 3 entries and remove the permission to create new entries in this specific section. I'm not sure if you can grant the permission to edit entries without the permission to store entries. Otherwise you'll need to hide/remove the button in the CP. There is a Craft.elementIndex in Javascript that has an sourceChange event. I'll show you how to do it in 3 days since I only have my smartphone right now. You'll need to pass a variable with all sections whether the user may create a new entry or not – Robin Schambach May 18 '18 at 15:41
  • Thank you Robin - that sounds like a very good option. I am fairly new to Craft and so it will take me some time to learn and work with this...... – ironfish May 18 '18 at 16:55

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.