4

I've structured my site so that most of the pages are in a single structure section (easier to manage that way and it means all my pages can be outputted and organised in the navigation's {% nav %} tag).

I've set up pages that have their own templates and would have been singles as new entry types (e.g. contact page, register form or members area) but it means that the user might try to add another one of those sections in, which I don't want them doing.

So is there a way to limit how many times an entry type can be used?

2

If you're working with frontend forms, set the permission to use any of these entry types to a specific user group or to admins only. Use isInGroup or the admin property:

{% if currentUser.admin %} ... {% endif %}
{% if currentUser.isInGroup('usersWithSomeEntryTypesHidden') %} ... {% endif %}

If you need this in the CP, you could make a plugin that does the same and hides selected entry types from the dropdown using JS. As this is no nice solution, what about making this a feature request to Pixel & Tonic?

  • Cheers carlcs, I will take a look at making a plugin for this first (got to start somewhere, right?) and if I struggle or decide it's really handy I will let them know about it! – Lighty_46 Nov 28 '14 at 13:52
  • If you don't need it right now, I'd definitely write them a mail first (maybe this exact feature already made it into Craft 2.3?), @Lighty_46. – carlcs Nov 28 '14 at 13:55
  • Cool, I've gotten in touch with them, if they come back with anything useful I will pop it on here. Thanks again @carlcs. – Lighty_46 Nov 28 '14 at 14:15
  • I'm wondering if this feature ever made it to the feedback site? – Ian Nebbiolo Jul 29 '15 at 10:21
  • @ianisted no, not until now: feedback.buildwithcraft.com/forums/285221-feature-requests/… – carlcs Jul 29 '15 at 10:54
2

If you make your own plugin, you can check the number of entries in a section before saving it using events. Following code would go in your main plugin file.

public function init() {
    craft()->on('entries.onBeforeSaveEntry', function(Event $event) {

        $criteria = craft()->elements->getCriteria(ElementType::Entry);
        $criteria->section = 'mySection';
        $criteria->status = 'null';
        $myEntries = $criteria->find();

        if (count($myEntries) > 99) {
            $event->performAction = false;
        }
    }
}

You could add an admin or permissions check if an admin should be able to add 101 entries.

1

This is such a good question! (I'm new to Craft, and am very surprised this isn't possible out-of-the-box.) I figured out a hacky but not-too-crazy way to hide entry types from the select menu (as opposed to an event that prevents it from being saved after it is selected, which is a bad user experience):

  1. Install this nifty Control Panel CSS plugin
  2. Figure out the entry type id (look in the database or inspect the source of the dashboard page and see what the option values are in the entry types dropdown).
  3. Add some CSS like the following to that plugin's CP page:

    #entry-form #entryType option:not(:checked)[value="4"] { display: none; }

  4. Add as many of those lines as you need for each of your entry types you want to hide.

Or if you'd rather go off the entry name instead of id (or you don't want to rely on those CSS3 selectors due to old browser support), you could achieve the same thing with some jquery, using the Control Panel JS plugin:

$(function() {
    var hiddenEntryTypes = [
        'Blog Index',
        'Employees Index',
        'Some other entry type name'
    ];
    $('#entry-form #entryType option:not(:selected)').each(function() {
        if (hiddenEntryTypes.indexOf($(this).html()) > -1) {
            $(this).hide();
        }
    });
});

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.