Creating date field with only month and year - Drupal - drupal

I am creating a credit card form in Drupal. I need a date select field where users can select their credit card expiry date, which consists of only Month and Year -No Day.
The Drupal form #type 'date' produces a date chooser, which has day-month-year option. I just need month-year. Any help?

Using the Date API module (part of the Date module), you can do something like this....
<?php
$form['payment_expirationDate'] = array(
'#type' => 'date_select',
'#title' => t('Expiration Date:'),
'#date_format' => 'm-Y',
'#default_value' => $expirationDate,
'#date_year_range' => '-1:+10',
'#required' => TRUE,
'#date_label_position' => 'within'
);
?>
The date_select type will give you select box form elements for your field. And the #date_format array key allows you to use a PHP date format string to control what select boxes appear and what goes inside them.

Go for two select boxes and pre populate the fields with month and years!
'#type' => 'select', '#options' =>
array(
'1' => 'January',
'2' => 'February',
.
.
.
'12' => 'December', ),
Same way for the years. You can generate the year array using simple php code and then use it in the option instead of giving all the years manually!

I've just done this in Drupal 7. Just install and enable the Date module, add a Date field to your Content Type, and then when editing it's settings, under "Date attributes to collect", just tick Year and Month. Then when a user is creating a new instance of that type, they will just get 2 dropdown boxes; one for Month and one for Year.
As for displaying them nicely, first go into Configutation > Date and Time settings, then create a new Format using PHP Date formatting e.g. "F Y" will give you something like "April 2012". Create a new Date Type which uses that format, and then go back to your Content Type display settings, and edit your new Date field to use the new Date Type.

Hey, I found the correct way of doing this. The solution creates a custom form #type element. See http://www.akchauhan.com/create-drupal-form-elements-like-date-element/

Related

Sort and paginate by custom date field with Wordpress / ACF / Timber

I'll try to describe what it is I want to achieve, please let me know if too much/not enough detail!
I'm making a site that needs to have documents uploaded to it. These documents will belong to one only of four categories and there will be one page per category. On this page, by default all documents will display. Users will also be able to view only the documents from each year by clicking a link.
It also needs to have pagination so a max of 10 documents are displayed per page.
So how I would ideally like to do this is to make a custom post type (documents) which would then display an ACF custom field set. The field set would have three fields (doc_type, doc_file and release_date). doc_type would be a select field of the four categories, doc_file a file upload and release_date a datepicker field.
When you click on a page, the controller would select by doc_type to only show the documents that belong to that category.
I want to order by release_date and use the year from this field as the display text of my links. For the actual href, I'm imagining something like /path/to/page.php?year=2017.
So there's a few problems here: first is that I am not sure if what I want to do is even possible - let alone a good way of doing things - with WordPress. I'm more used to pure PHP and mySQL, and so I might be trying to shoehorn more general solutions into WP. Can anyone confirm if it's possible to achieve what I want?
Second, I'm specifically running into some issues when I try to execute this plan.
I can successfully get each page to only display the documents that belong to it, like so (although I'm generating $last by grabbing info from the URI, which seems dodgy).
$docArgs = array(
'post_type' => 'documents',
'meta_key' => 'doc_type',
'meta_value' => $last,
);
$context['documents'] = Timber::get_posts($docArgs);
When I try to get my sorting on though, de nada:
$docArgs = array(
'post_type' => 'documents',
'meta_key' => 'doc_type',
'meta_value' => $last,
'meta_query' => array(
array(
'key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => DESC,
),
),
);
I assume I've got the syntax wrong, but I'm basically really confused about the whole thing. Any suggestions?

show month number instead of month name on symfony2 formbuilder

It might be ridiculous quesion, but I can't find the way with googling.
I am making form like this.
$form = $this->createFormBuilder($searchTime)
->add('date','date',array(
"property_path" => false,
'years' => $years,
'data' => new \Datetime()
but it shows month select box as name such as jun,july,aug
however I want to show number here 6,7,8 ....
How can I make it ?
Maybe it's worth a try to specify a format, as the reference says:
$builder->add('date', 'date', array(
'widget' => 'choice',
'format' => 'yyyy-MM-dd',
// ...
));
The reference says, that if your widget is choice and you specify the format then the selects will be rendered in the order of the format. Maybe the select format is also will be adjusted to the format pattern.
(The month without leading zeros would be: yyyy-M-dd)

How to set minimum year value in Sonata Admin Bundle date form field

I'm using Sonata Admin Bundle with Symfony2. I have a datetime field. This is my piece of code. Pretty simple.
->add('passportIssueDate', 'date')
But in the form I can select Year value only from 2008 to 2018.
Is there a way to set different range?
Thanks in advance.
You would use something like this:
$builder->add('dob', 'birthday', array('years' => range(1980, date('Y')), 'format' => 'dd-MMMM-yyyy'));
This means the Date Field (regardless of if it's DateTime, Date, or Birthday) will give you a range from 1980 in this case to the current year. To get a range including years in the future, you would use something like this:
$builder->add('startDate', 'date', array('years' => range(2012, 2020), 'format' => 'dd-MMMM-yyyy'));
This instance gives you a range from 2012 to 2010.
Furthermore, the 'format' => 'dd-MMMM-yyyy' will format your date selection.

Drupal 7 db_insert on WYSIWYG textarea field

this is my first post here at stackoverflow and i'll try make sharp and short ;-)
I am running into problems when inserting a textarea form field using CKeditor into the database.
Here is my form element:
$form['add']['description'] = array(
'#wysiwyg' => true,
'#name' => 'description',
'#title' => t('description'),
'#type' => 'text_format',
'#base_type' => 'textarea',
);
When submitting the form i do get a SQL error like this because Drupal appends the placeholder elements 'value' and 'format'
db_insert failed. Message = SQLSTATE[21S01]:
Insert value list does not match column list:
1136 Column count doesn't match value count at row 1,
query= INSERT INTO {tablename} (description)
VALUES (:db_insert_placeholder_13_value, :db_insert_placeholder_13_format)
Unfortunately i exactly have to set up the textarea this way to make the CKeditor work. Can you advice me how to get rid of the
:db_insert_placeholder_13_value,
:db_insert_placeholder_13_format
to a single variable again?
I really appriciate your help
PROBLEM SOLVED --- silly me, should have used my glasses more :-D
Just use the correct array index inside the submit hook for saving the data to the database and it will work:
function MYMODULE_form_add_submit($form, &$form_state) {
...
'description' => $form_state['values']['description']['value'],
...
}
If you're just interested in the text itself (not the format), add a validation handler for the form and use something like this
function MYMODULE_form_name_validate($form, &$form_state) {
$form_state['values']['description'] = $form_state['values']['description']['value'];
}

How to hide Date of birth field in overriden form?

In my hook form alter, this is the date of birth array:
[Birthdate] => Array(
[#type] => fieldset
[#title] => Birthdate
[#weight] => 1
[profile_birthdate] => Array(
[#type] => date
[#title] => Birthdate
[#default_value] =>
[#description] => The content of this field is kept private and will not be shown publicly.
[#required] => 1
)
)
I have tried:
unset($form['Birthdate']['profile_birthdate']);
unset($form['Birthdate']);
and that does not work as I still get the "Please enter a valid date of birth" message. I want the field hidden and no message.
The phrasing of the error message ('... a valid date...') hints on this being issued from a form validation function. Depending on how that is written, it might still try to validate the field, even if you removed it successfully from the form.
If that is the case, you'll need to either override the validation function with a custom version that does not expect the birthdate field (check the content of $form['#validate'] in your hook_form_alter() implementation). Alternatively, instead of removing the field, you could turn it into '#type' => 'hidden' or '#type' => 'value', and provide a default value that passes validation, but then you'll end up assigning bogus birth dates, which might not be what you want.

Resources