Hi guys,

I have been searching for hours for info on this. Forgive me, I'm a beginner trying to learn PHP at the same time as tackling Drupal's form API. It's all new and I'm just looking for a bit of advice.

I am overriding my "Authored by:" field, so that it's non-editable, that much is done, but I can't seem to find where the original values for stuff like the #default_value is held in order to copy to my override function. This is resulting in me having an empty box. Ideally, I'd like to keep the existing functionality of having the user name appear in this box. How do I declare that in my code below?

$form['author']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => '60',
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => $user, // << This is the problem!
    '#weight' => '-1',
    '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
    '#attributes' => array('disabled' => 'disabled'),
);

Many thanks for your time,
Jonathan

Comments

jaypan’s picture

You can do this with hook_form_alter(). I believe the following should do it for you.

mymodule_form_alter(&$form, &$form_state, $form_id)
{
  if(isset($form['author']['name']))
  {
    $form['author']['name']['#type'] = 'value';
  }
}

Contact me to contract me for D7 -> D10/11 migrations.

jonthomas83’s picture

Hi, thanks for your help. The code you posted doesn't seem to work unfortunately. I posted what I wanted to change above but here's the full piece of code I've got.

function mymodule_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
    	case 'page_node_form':
			$form['author']['name'] = array(
                '#type' => 'textfield',
                '#title' => t('Authored by'),
                '#maxlength' => '60',
                '#autocomplete_path' => 'user/autocomplete',
                '#default_value' => $user, // <<< This is the value I need
                '#weight' => '-1',
                '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
				'#attributes' => array('disabled' => 'disabled'),
			);
        break;
  }
}

I've commented the piece of code I need. Basically, in this disabled field I need it to say the users user name - for example "jonthomas83". But it'll be non-editable, which my code above already ensures. Thing is I don't know how to display the user name in there.

Many thanks.

jaypan’s picture

Ahh, I thought you just wanted to hide it altogether. I didn't realize you still wanted to show it to the user. In that case this should do it for you (assuming it's the current user's name that you want to be displayed):

function mymodule_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
        case 'page_node_form':
            global $user;
            $form['author']['name']['#default_value'] => $user->name;
            $form['author']['name']['#disabled'] => TRUE;
        break;
  }
}

You don't need the other elements because they have already been set. hook_form_alter() alters the definition of the form. Therefor you don't need to re-declare anything that you are not altering.

Contact me to contract me for D7 -> D10/11 migrations.

jonthomas83’s picture

Thanks so much for taking the time to help me out, sorry my first post didn't explain properly.

Yes, you hit the nail on the head with what I want to achieve. A 'non-editable', disabled field with a string of the currents user's user name in it. Basically, so they can't alter who wrote the content, instead it automatically defaults as them.

Unfortunately though, the code still didn't work. I had wondered why my current code wasn't working when I just declared the things that I wanted different to the default attributes of the field. I found when writing my code I had to declare everything. Your way makes much more sense, as I'd only be declaring what needs to be changed. However, it didn't quite work and I still got the white screen of death!

However, I implemented the 'global' into my existing code and it works brilliantly! Thank you! Still unsure why everything needs to be rewritten, I'd have thought it would have cascaded the default behaviours, but nevertheless, here's what I have now:

function grapevine_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
    	case 'page_node_form':
			// return print_r($form);
			// $form['revision_information']['#title'] = t('Revisional Stuff Innit!');
            global $user;
			$form['author']['name'] = array(
                '#type' => 'textfield',
                '#title' => t('Authored by'),
                '#maxlength' => '60',
                '#default_value' => $user->name,
                '#weight' => '-1',
                '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
				'#attributes' => array('disabled' => 'disabled'),
			);
        break;
  }
}

Thanks again!

jaypan’s picture

Just to let you know, the very first solution I gave to you should have hidden the field altogether so that the user couldn't see it, but still saved the value as it was supposed to. I'm not 100% sure on that - I didn't test it, but I've done similar things in the past and it has worked.

Contact me to contract me for D7 -> D10/11 migrations.

jonthomas83’s picture

Thanks that's great. I'll give it another test on Monday, but I'm pretty sure it gave me a white screen so I'll see what I need to do to get it to work and will post back here. Many thanks.