Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » DataObject plural_name() Method Truncates 'e'?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 222 Views
  • HARVS1789UK
    Avatar
    Community Member
    20 Posts

    DataObject plural_name() Method Truncates 'e'? Link to this post

    There is a method on DataObject which returns a 'best guess' plural name of you data object, see - http://api.silverstripe.org/3.0/source-class-DataObject.html#659-676

    $name = $this->singular_name();
    if(substr($name,-1) == 'e') $name = substr($name,0,-1);
    else if(substr($name,-1) == 'y') $name = substr($name,0,-1) . 'ie';
    return ucfirst($name . 's');

    This is used by GridFields 'Link Existing' auto completer. I am confused by the nested if() in this method. I understand the purpose of truncating a 'y' and replacing with 'ies' e.g. Party -> Parties etc.

    The first part of the if() checks to see if the name ends with 'e', if so it truncates it (and then proceeds to make it plural by adding 's')!

    I cannot think of any good reason for this? Perhaps I am missing something but I do not see why this line of code exists? It is returning 'Carousel Slids' instread of 'Carousel Slides' for one of my data objects. Other incorrect examples it would return include:

    - Tree = Tres
    - Page = Pags
    - Attribute = Attributs
    - etc

    I know I can solve this issue by adding a 'private static $plural_name = "Carousel Slides";' on my data object, but thats not the point, I am trying to understand if there is any logical reason for this line of code, or if it should be marked as a bug?

    Any suggestions appreciated.

    Cheers,

    HARVS1789UK

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: DataObject plural_name() Method Truncates 'e'? Link to this post

    Interesting I can see how this wouldn't work at all for other locales besides en_US !

    I'm all for just returning the singular name, that is at least a valid name, even if not syntactically correct in the context. And maybe update the documentation about the importance of setting your own plural_name?

    Martine

  • HARVS1789UK
    Avatar
    Community Member
    20 Posts

    Re: DataObject plural_name() Method Truncates 'e'? Link to this post

    Thanks for the reply Martimiz,

    Out of pure curiosity can you give me some examples of how this does work for words in en_US locale?

    I'm just trying to work out how truncating the 'e' is beneficial in any locale and am struggling to come up with any examples where it works?

    Cheers,

    HARVS1789UK

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: DataObject plural_name() Method Truncates 'e'? Link to this post

    haha - no! I meant to agree with you on the e, and then go even further by saying that replacing an y for an ie might not always be a good idea in, say, Dutch

    222 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.