I would like to put a quite long HTML code as "Anonymous link text" in the settings page (/admin/structure/flags/manage/myflag). It accepts HTML, but it seems to be limited to 128 characters.

How can I change it to accept more characters?

I've found https://www.drupal.org/project/maxlength but I don't know it that would really work for this and anyway I prefer not to install yet another module just for this.

BTW, I could be interested in the future in doing the same for other fields (i.e. "Flag link text") and I guess it could be useful for other people, so a "general solution" would be great, but I only need it right now for "Anonymous link text".

Thanks a lot!!

Comments

delvalle created an issue. See original summary.

delvalle’s picture

Nobody?

joachim’s picture

> How can I change it to accept more characters?

Possibly a hook_form_alter(), and change the properties of the form element.

delvalle’s picture

Thanks Joachim!

I'm not a developer, but I've followed the instructions of https://www.drupal.org/node/1561658 and created a custom module with the following code in the .module file:

<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'edit-flag-anon'){
    $form['edit-anon-message']['#language']['#maxlength'] = '220';
  }}

It doesn't work, activating the custom module doesn't change anything.

I think I've followed those instructions correctly and adapted the fields to the correct IDs, but I'm probably missing something.

joachim’s picture

The form ID will have underscores, not hyphens, because it must form part of a valid PHP function name.

delvalle’s picture

Thanks, I've corrected that and also corrected the form ID (I think I wasn't choosing the right one). I've tried also the following code (removing the ['#language']) but nothing works, it doesn't change the maxlength. It must be something else.

<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'flag_form'){
    $form['edit_anon_message']['#maxlength'] = '220';
  }}
joachim’s picture

Install Devel module if you haven't already.

To check form IDs, do:

dsm($form_id);

in your hook. If you don't see any output, check Devel module permissions.

Once you have the right form ID, do

dsm($form)

to check which part of the form to alter.

delvalle’s picture

I thought it was enough to look at the IDs in the HTML (source code).

I've installed Devel, but I'm not sure how to use it (the guides I've seen are not exactly beginner-friendly).

I tried the code you recommended (dsm($form_id);) in my custom module and also in the "execute PHP" block that Devel provides when visiting the page/form I want to modify (https://www.aventurazas.com/admin/structure/flags/manage/MYFLAG). It gives me errors in both cases.

The Execute PHP error says:
Notice: Undefined variable: form_id en eval() (línea 1 de MYSITE/sites/all/modules/devel/devel.module(1429) : eval()'d code).

joachim’s picture

It's pointless to do that in the Execute PHP. There's no form at that point!

You need to run it in your hook, to check your hook is firing, and to check the form ID.

delvalle’s picture

Sorry, but I'm not a developer, I have not idea what I'm doing.

I've put dsm($form_id); inside the hook and it says "flag_form" (so the ID I was using is OK).

Then I've put dsm($form); (I figured out I had to add a semicolon to your code) inside the "if($form_id..." and it shows different info from Devel. In "#Form ID" it shows "flag_form" and in "Form ID" it shows "edit-flag-form".

So I tried changing the module code to:

<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'flag_form'){
    $form['edit-flag-form']['#maxlength'] = '220';
  }}

But still nothing. I've also tried with edit_flag_form (underscores) and change "mymodule" for the real name of my module, but nothing works.

It's my first custom module and I'm not a developer. I guess I could spend dozens of hours trying things randomly and still get nothing, so I would really appreciate detailed guidance here. Otherwise I guess I will have to look for a different solution/module for my needs.

joachim’s picture

> Then I've put dsm($form); (I figured out I had to add a semicolon to your code) inside the "if($form_id..." and it shows different info from Devel

You need to inspect the data that's output at that point, to find the right part of the form array to alter. Look at the keys of the array and find the form element you want to change.

delvalle’s picture

I've looked at the array and I don't think that's the problem (I've tried with other field, easier to identify, and it doesn't work either), but I'm not really sure, it's my first time with Devel and "advanced" Drupal stuff.

Are you certain that the code I provided will work with the appropiate IDs?
I don't even know if I have to put "mymodule" or something else, if I have to add the "['#language']" part or even if the code works at all (it comes from https://www.drupal.org/node/1561658 and even there it says "not tested!" at the end).

I'm willing to try whatever you tell me, but it would be a pity if we are both losing our time with the IDs and the problem is somewhere else...

joachim’s picture

> Are you certain that the code I provided will work with the appropiate IDs?
I don't even know if I have to put "mymodule" or something else

You're picking apart stuff that you've already got working!

If you saw output from your dsm() call in your hook, then the hook works. Now move on to the next step.

You need to find the right part of the form to change.

delvalle’s picture

I'm not picking apart anything, It's the same code as before, but it doesn't seem to work with any form element (I've tried already like 10 combinations), so I don't know what is wrong with it. Maybe it's the "['#maxlength']" part or something else.

If you can't give me more specific advice or clues, I think I'm giving up, I don't know what else to try.

joachim’s picture

> but it doesn't seem to work with any form element (I've tried already like 10 combinations), so I don't know what is wrong with it. Maybe it's the "['#maxlength']" part or something else.

Don't blindly try everything.

Look through the form array and find the RIGHT form element -- you can spot it by the title and description.

Look up its type, and find the documentation for that type. It could be that that type doesn't allow a length longer than 128 anyway.

delvalle’s picture

I've already tried what I think is the right form element (with several possibilities and variations like underscores) and it's not working.

Maybe I'm doing something wrong, there is something wrong with the code or that type doesn't allow longer length. I clearly don't have the knowledge to find out what is the problem, so I'm giving up.

Thanks anyway for your time Joachim.

joachim’s picture

> I've already tried what I think is the right form element (with several possibilities and variations like underscores) and it's not working.

You don't need to guess at underscores. The element you want is shown for you in the output when you do dsm($form).

ivnish’s picture

Status: Active » Closed (outdated)

Closed as outdated because Drupal 7 is EOL