Disable a Field in a Paragraphs Form - drupal

Does anyone know how to alter a field in a paragraphs (ajax) backend form in Drupal 8? I want to disable a field, but keep it visible.
Thanks

function hook__form_FORM_ID_alter(&$form,\Drupal\Core\Form\FormStateInterface
$form_state, $form_id) {
//output your form structure to know what to target in the form array($form[])
#kint( $form['title']);
$form['title']['#disabled'] = TRUE;
}
The above code disables the title field (Drupal 8.5) in the 'FORM_ID' you want to modify.

You can disable a form field by either using hook_form_alter() or by hook_form_FORM_ID_alter().
I would always suggest you to use hook_form_FORM_ID_alter(). Suppose test is your modules name and user_register_form is the Id of the form.
test_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['fieldname'] = array(
'#type' => 'textfield',
'#title' => t('Text label'),
'#attributes' => array('disabled' => 'disabled'),
);
}
Happy coding!!!

Related

Drupal login form customize

Hi i have used this code
<?php
$elements = drupal_get_form("user_login");
$form = drupal_render($elements);
echo $form;
?>
to get the default Drupal login form for my site but I need to customize the HTML, I have found some pages in module/users but did not understand how to customize the structure.
The user login form for Drupal is built by the user_login function in user.module using Drupal Form API. If you need to customize it, you should do it using hook_form_alter() in your module
function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id=='user_login') {
// YOUR CUSTOM CODE FOR THE FORM GOES HERE
}
}
** EDIT, AFTER YOUR COMMENT **
You don't need to call the YOUR_MODULE_NAME_form_alter() function: Drupal does that for you via the hook mechanism everytime it needs to build a form, and, when $form_id=='user_login', it modifies the login form to allow your customization. The way Drupal does that is discussed in detail in drupal.org, just follow the link I wrote at the beginning of this answer.
The user login form is declared this way in user.module:
// Display login form:
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
);
$form['name']['#description'] = t('Enter your #s username.', array('#s' => variable_get('site_name', 'Drupal')));
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
);
$form['#validate'] = user_login_default_validators();
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
The $form array is passed by reference to your hook_form_alter() before being rendered, allowing for customization. So, let's say that you want to change the label of the textfield for the user name from "Username" to "Name of the User", you write
$form['name']['#title'] = t("Name of the User");
in your custom code. If you want to add another field to the form (a textarea, for example), you do
$form['otherfield'] = array(
'#title' => t('My new custom textarea'),
'#type' => 'textarea',
'#description' => t("A description of what this area is for"),
'#cols' => 10,
'#rows' => 3,
'#weight' => 20,
);
and Drupal will add the field to the user login form.
There are many different kind of fields and properties that you can customize this way: I encourage you to fully read the Form API documentation. This way you let Drupal take care of form generation, translation, rendering, validation and submission, also permitting to other modules to manipulate your form if needed.
I hope it's clear, have a good day.
use this in template.php
function themename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'corporateclean') . '/templates',
'template' => 'user-login',
);
and create a template folder and within that create a file user-login.tpl.php and in this file you can put your html and could customize drupal login

Drupal fields: Passing a variable to view

I have a custom field plugin. It's widget looks somewhat like this:
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
...
$main_widget = $element + array(
'#test' => 'test',
'#type' => 'textarea',
'#attributes' => array('item_capacity' => 3),
'#theme'=>'mymodule_theme'
);
...
}
I need to get a value of another field, attached to current node, and pass it to this widget's theme file. Can i somehow get at least an id of a node that contains current field from hook_field_widget_form()?
You can use the module Devel to print the value of the $form array which will contain the form information you are looking for.
Example:
dpm($form);
You will be able to easily see the content of the form and access it with php in array form.

Drupal 7 Custom Module | Create a button in settings page that calls a custom function

I've created a custom module with it's own settings page where the administrator can set certain parameters (radio buttons, input fields and dropdown).
I'm looking for a way to create a button in the form that will perform a custom function.
(Example: Like the 'clear all caches' button in the admin page.)
What's the easiest or best way to create this?
Kind regards and thanks in advance!
Each different button on your form can perform a different function by assigning it the #submit property:
function mymodule_my_form($form, &$form_state) {
$form['button1'] = array(
'#type' => 'submit',
'#value' => 'Perform 1st Action',
'#submit' => array('mymodule_my_form_action_one')
);
$form['button2'] = array(
'#type' => 'submit',
'#value' => 'Perform 2nd Action',
'#submit' => array('mymodule_my_form_action_two')
);
}
function mymodule_my_form_action_one($form, &$form_state) {
// Perform the 1st action
}
function mymodule_my_form_action_two($form, &$form_state) {
// Perform the 2nd action
}
Only the specified submit function will run for each button click.

Drupal hook_form_alter Not redirecting

I am using hook form_alter and setting the $form['#redirect'] = 'dir1/dir2/mypage'. But the form refuses to go there.
Form seems to work otherwise, but keeps sending back to the original form instead of the redirect.
The form I am altering is from the root user module.
mymodule_form_alter( ){
... code...
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25, '#description' => t(''),
'#required' => TRUE
);
unset($form['Birthdate']['profile_birthdate']);
unset($form['Birthdate']);
unset($form['#action']);
$form['#validate'] = array('_mymodule_school_registration_validate');
$form['#submit'] = array( '_mymodule_school_registration_submit');
$form['#redirect']= "dir1/dir2/mypage";
}
Please help trying to meet an overdue dead line!! : (
Thanks in advance.
Your hook_form_alter() implementation is not correct:
Without parameters, you're aren't modifying anything, so none of your changes get registered,
$form['#submit'] and $form['#validate'] are already arrays with content, so you should not be resetting them with array(),
unsetting $form['#action'] causes the form to do nothing when submitted,
setting $form['#redirect'] in hook_form_alter() will get overridden by other handlers, and
your hook_form_alter() implementation would affect (and break) every form.
More info: Forms API Reference
Instead, try the following:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'form_id_goes_here') {
// Code here
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#description' => t(''),
'#required' => TRUE
);
unset($form['Birthdate']['profile_birthdate']);
unset($form['Birthdate']);
$form['#validate'][] = '_mymodule_school_registration_validate';
$form['#submit'][] = '_mymodule_school_registration_submit';
}
}
function _mymodule_school_registration_submit($form, &$form_state) {
// Code here
$form_state['redirect'] = 'dir1/dir2/mypage';
}
Try
$form_state['redirect'] = "dir1/dir2/mypage";
If you've only got one handler for your submit you could easily redirect with
function _mymodule_school_registration_submit(..args...) {
...
drupal_goto('somewhere');
}
I think the functionality is the same.
http://api.drupal.org/api/function/drupal_goto/6
I tend to avoid redirects so once you have met your deadline I would refactor your code. You can usually get away with out redirecting.

Overriding the user registration form in Drupal 6

I want to be able to customise the user registration form in Drupal 6
I have found thousands of tutorials that show me how to override the structure in which you can output the form as a whole, but I want to move form elements around etc and I cant quite seem to see the best way to do this
To expand on Jeremy's answer, you're going to want to study Drupal's Form API and user_register(). In short, you build an associated array; each element in the array corresponds to one form element.
Each form element in the array is its own associated array. They can have a type: textfield, select menu, checkboxes, etc.: see the Form API reference for all the types.
Each form element can also have a weight: this is how you order elements around. Lower numbered weights show up before higher numbered weights in the form.
One of the element types available to you is fieldset: this is what will allow you to group elements together. When you use a fieldset, it creates a section of the form with its own weight values.
So, let's say you have a form with three fields: Name, Company, and E-mail address. The Name should show up first, Company second, E-mail address third. You could specify the form like so:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note the #weight key. If you wanted Company to appear after E-mail address, you'd set $form['company']['#weight'] to something higher than 3.
Now let's say you wanted to group Name and Company into a fieldset called Personal Information. Your form would now look something like this:
$form['personal'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#weight' => 1,
);
$form['personal']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['personal']['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note that Name and Company are now array elements of $form['personal'].
If you want to make Name show up after Company in the fieldset, set its #weight higher than 2. Because the Name is now part of a fieldset that has a lower #weight than the E-mail address field, even if you set $form['personal']['name']['#weight'] to 4, it wouldn't make the Name show up after E-mail address.
So what you're going to attempt to do is use hook_form_alter() to alter the user_register form to change the weights of certain form elements, create your own fieldsets, and move certain form elements into your newly created fieldsets.
There are ways to do this within your theme, but I prefer creating a custom module for this. Create your custom module, and implement hook_form_alter():
function test_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user_register') { // Only modify the user registration form
// Before you can get down to business, you need to figure out the
// structure of the user registration form. Use var_dump or kpr to dump
// the $form array.
// Note: if you want to use kpr on the user registration form, give
// anonymous permission to see devel information.
// kpr($form);
// Move Name field to after E-Mail field
$form['name']['#weight'] = 2;
$form['mail']['#weight'] = 1;
// Group Name and E-mail together into a fieldset
$form['personal_info'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
);
$form['personal_info']['name'] = $form['name'];
$form['personal_info']['mail'] = $form['mail'];
// The last block only copied the elements: unset the old ones.
unset($form['name']);
unset($form['mail']);
}
}
In more complex forms, moving things from one fieldset to another might yield unexpected results when submitting the form. This is because $form['name'] isn't the same as $form['group']['name'], which isn't the same as $form['other_group']['name']. You don't have to worry about that on the user_register form for the most part, but check out the handbook page on #tree and #parents for more information about this.
This covers modifying existing fields in the user registration form: if you want to add new fields, I highly recommend using Content Profile. If you want to create custom fields on your own, it's going to get a lot more complex as you're going to have to implement your own validate and submit handlers. Content Profile handles this for you: check out its README to see how to activate it for registration forms.
Using hook_form_alter you can do whatever you want with a form.
For example changing the weight can change the position on the page.
If you try:
MYMODULE_form_user_profile_form_alter(&$form, $form_state) {
// do your processing here
var_dump($form);
}
replacing MYMODULE with the name of your module.
You will see the structure of the form, you can change values in there to alter, labels weights descriptions etc.
In a module, first use hook_theme() , now assuming the name of your module is 'd6_forms' :
function d6_forms_theme() {
return array(
'user_register' => array(
'template' => 'templates/user-register-form',
'arguments' => array('form' => NULL),
),
);
}
This will make the user_register form look for a template, in the specified folder.
So make sure that in your module folder, there is a folder called 'templates', with a file 'user-register-form.tpl.php'.
You notice that in the hook_theme() , the extenstion of the template file ( .tpl.php ) is not supplied. That's normal, you don't need to specify it there.
Do make sure however, that the template has that extension, and that it's not just named 'user-register-form.php' !
In that template file, you have access to the $form variable , so print it there to see what fields are in there.
The devel module is recommened, since it's able to print big Drupal arrays in a fancy way ( using dpm() ).
If you do not have Devel module, or don't want to use it, this also works : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.
To print a field, just use <?php print drupal_render($form[field_name]); ?>, this will print the field and make sure that it works as intended.
So for example, if you want to print the 'name' field in the $form array, just use <?php print drupal_render($form['name']); ?>.
You don't have to print every field ! Just print the fields that you want to move somewhere ( which, with a basic Drupal register form, are about 3 : name, email & submit ).
To print all the remaining fields, just end your template with <?php print drupal_render($form); ?>.
It is important that you don't forget this, since the $form var contains stuff that is absolutely needed for your form to work ( like a token, etc .. ).
So good standard behaviour when templating a form, is to print that piece of code first at the bottom of your template.
This is an entire example of a small register form template, with some basic html :
<?php
// What is in that $form var ? To check, uncomment next line
// print '<pre>' . print_r($form, 1) . '</pre>';
?>
<div style="background-color:#ddd;padding:10px;">
<?php print drupal_render($form['name']); ?>
<?php print drupal_render($form['mail']); ?>
</div>
<div>
<?php print drupal_render($form['submit']); ?>
</div>
<?php print drupal_render($form); ?>
maybe this will help:
http://drupal.org/node/44910
You just need to Enable the Profile Module which would give access to place more fields in sign up form.
Go through this Simple Video tutorial which would be very helpful for beginners in Drupal .
http://planetghost.com/add_more_fields_to_sign_up

Resources