elliot-sawyer / silverstripe-linkfield

Adds a Linkfield for gorriecoe/silverstripe-link
BSD 3-Clause "New" or "Revised" License
9 stars 25 forks source link

How to add a max number of Links? Does Linkfield have any configuration to support it? #37

Open sephyhe opened 1 year ago

sephyhe commented 1 year ago

Hi there,

I was wondering if you could kindly let me know if Linkfield supports a maximum number of links? I understand that having a maximum number of links could be quite useful in preventing any potential issues with breaking the FED layout.

I have attempted to find related information in the source code, however, I was unable to locate any. Would it be possible to add a parameter to support this feature and utilize it to remove the "add new" button when the gridfield reaches the maximum number? If you feel that this is a viable solution, I would be more than happy to create a pull request for you.

Thank you

elliot-sawyer commented 1 year ago

There is no maximum number of links. Best way to enforce this would be within your own project code. I've done this in the past by placing a .Limit(N) rule in my loop, for example:

<% loop MyLinks.Limit(5) %>
...
<% end_loop %>

You can use the drag/drop ordering to manage the list in the CMS, and add a description reminding CMS users that only the first N items will be shown on the website. That way they can actually add as many as they'd like.

GuySartorelli commented 1 year ago

The LinkField, when used with a many relation, is effectively a gridfield. You might be able to use https://github.com/fromholdio/silverstripe-gridfield-limiter or https://github.com/webbuilders-group/silverstripe-limitedrelationsgridfield (or adapt some of the code if neither of those work out of the gate) to limit the number of links that can be added to the field.