Jump to content

Truncate page name to whole word


adrian
 Share

Recommended Posts

Hey Ryan,

When entering page titles for things like news articles that have very long titles, these often exceed 128 characters. I think it would be nicer if PW truncated to a whole word, rather than possibly one or a few letters of the last word after a dash.

I know the client could manually edit the name, but they never pay attention to things like this :)

Thanks for considering,

Adrian

Link to comment
Share on other sites

it would be sort of easy to do this with a hook, for example you could have a field near the title, called something like "short title" and then the user could come up with a good short title, and then your hook could set that field as the page name on save...  just did something like this recently where I needed the page name (url) to be kept in sync with another custom field, during development...

Link to comment
Share on other sites

Hey Macrura,

Thanks for the suggestions. Lots of simple modules ways of doing this - could probably even override the default name on save, but I was hoping Ryan might consider it a useful addition to the core.

I am not too worried about it for my needs, but I think it would be a nice refinement still.

Link to comment
Share on other sites

Hey Ryan,

Using the following in InputfieldPageName.js seems to do the trick.

if(name.length > 128) name = $.trim(title).substring(0, 128).trim(this);

I was looking at InputfieldPageTitle.js and noticed that it doesn't actually do the truncate to 128. The browser is truncating to 255, but I think it is missing something like:

$titleField.val(val).trigger('blur');

Also, I am not sure why, but the approach in the first code block above doesn't work for the title. Instead, I had to use:

if($titleField.val().length > 128) val = $.trim($titleField.val()).substring(0, 128).split(" ").slice(0, -1).join(" ");

But, I guess I am not sure what you actually want to do with the title field. Do you want it truncated to 255, or 128 to match the length of the name field?

Some potentially more robust ways of doing this:

https://github.com/micjamking/succinct/blob/master/jQuery.succinct.js

https://gist.github.com/ChrisCinelli/5688048

Neither tested so far, as I don't think we need them. In particular, the succinct plugin was designed to deal with a couple of edge cases that I don't think should affect us: http://stackoverflow.com/questions/4637942/how-can-i-truncate-a-string-in-jquery

  • Like 1
Link to comment
Share on other sites

Thanks Adrian, this looks like a good solution. Where exactly did you put it? I'm not clear about the full context of where it goes. 

But, I guess I am not sure what you actually want to do with the title field. Do you want it truncated to 255, or 128 to match the length of the name field?

The title field should never be truncated. But the name field has a maximum length of 128 characters. 

Link to comment
Share on other sites

Hey Ryan,

Little bit of confusion on my part - I was using an older version of PW to test these changes. Now testing on dev, so this should all work fine.

Regarding the title field - it is being truncated by the input field:

<input id="Inputfield_title" class="required InputfieldMaxWidth" name="title" type="text" maxlength="255" />

Of course the DB field "data" is a full text, so it is not being truncated there, so firstly, can we remove the maxlength from the input field for the title?

Also, there is a line in InputfieldPageTitle.js which is designed to truncate "var" to 128 characters.

So, what I have done in InputfieldPageTitle.js is:

        //var val = $(this).val().substring(0, 128);
        var val = $(this).val();

Then in InputfieldPageName.js:

//if(name.length > 128) name = name.substring(0, 128);
if(name.length > 128) name = $.trim(name).substring(0, 128).split("-").slice(0, -1).join(" ");

Does that all makes sense?

Would you rather I submitted this as a pull request?

Link to comment
Share on other sites

Regarding the title field - it is being truncated by the input field:<input id="Inputfield_title" class="required InputfieldMaxWidth" name="title" type="text" maxlength="255" />Of course the DB field "data" is a full text, so it is not being truncated there, so firstly, can we remove the maxlength from the input field for the title?

u set.maxlen in feild settins 

setup..feilds..title..input

Link to comment
Share on other sites

Thanks Willy,

I guess I am suggesting that Ryan remove the 255 limit since this is the default for the built in title field. 

Admittedly long titles like these are somewhat of an unusual case, but I have a few sites where the client is entering newspaper articles and scientific journal articles and the titles of these can be very long and I don't want them truncated. Ryan mentioned that the title should never be truncated, so just trying to ensure that this actually is the case by default.

Link to comment
Share on other sites

  • 3 weeks later...

Hey Ryan,

Any thoughts on incorporating those changes in post#7?

I am actually using this on a couple of sites now and if you aren't planning on doing something like this for the core, then I might start thinking about a module.

No pressure, just want to make sure I don't forget about this when I do a PW upgrade :)

Thanks,

Adrian

  • Like 1
Link to comment
Share on other sites

Adrian, regarding 'name': I like your solution in post #7. I've added it to my code and will test locally and commit it soon if all works well.

Regarding the 'title': There is no 255 limit for 'title'. Like WillyC mentioned, the only limits to title are those that you introduce in the field settings. If your title is getting truncated at 255 chars, then it's likely configured for a max length of 255 chars. The default max length setting for title is 2048, but this can be increased or decreased as desired in the field settings. 

Link to comment
Share on other sites

Hey Ryan,

Thanks very much for taking care of this change.

Regarding the title limit - every time I have installed PW with the default profile or the new Foundation profile, the title field already has maxlength set to 255, not 2048.

Here is the relevant line in the install.sql file:

INSERT INTO `fields` (`id`, `type`, `name`, `flags`, `label`, `data`) VALUES (1,'FieldtypePageTitle','title',13,'Title','{\"required\":1,\"textformatters\":[\"TextformatterEntities\"],\"size\":0,\"maxlength\":255}');
 

Also, I am not really sure how relevant this is:

CREATE TABLE `field_title` (
  `pages_id` int(10) unsigned NOT NULL,
  `data` text NOT NULL,
  PRIMARY KEY  (`pages_id`),
  KEY `data_exact` (`data`(255)),
  FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 

I know it is only setting the data_exact key and not the field length, but is there any reason to limit this to 255?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...