Skip to content

Instantly share code, notes, and snippets.

@init90
init90 / cba_quiz.module
Last active May 5, 2018 21:22
Drupal 7. Quiz module. Add programality new questions to quiz.
/**
* Create new multichoice questions.
*/
function cba_quiz_new_multichoice_question() {
global $user;
$question = new stdClass();
$question->title = t('Questions template');
$question->type = 'multichoice';
$question->body = array(LANGUAGE_NONE => array(array('value' => t('Your questions text'))));
@init90
init90 / scratch.txt
Created December 14, 2016 08:42
drupal 7, views data alter
CREATE TABLE example_table (
nid INT(11) NOT NULL,
plain_text_field VARCHAR(32,
numeric_field INT(11),
boolean_field INT(1),
timestamp_field INT(8),
PRIMARY KEY(nid)
);
function mymodule_views_data() {
@init90
init90 / gist:cfb51b5943a155536ac1da4dfb5cc12b
Last active May 5, 2018 21:16
Drupal 7, get field allowed values.
$fields = field_info_fields();
$races_list = list_allowed_values($fields['field_race']);
@init90
init90 / cba_events.confirm_popup.inc
Created December 22, 2016 13:58
drupal 7, form button for hide ctools popup.
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#attributes' => array('onClick' => 'Drupal.CTools.Modal.dismiss();'),
);
@init90
init90 / scratch.txt
Created December 22, 2016 15:38
drupal 7, add date field with popup calendar to custom form.
$form['startdate'] = array(
'#type' => 'date_popup',
'#title' => t('Lesson start date'),
'#timepicker' => 'timepicker',
'#timepicker_options' => array(
'rows' => 6,
'minutes' => array(
'starts' => 0,
'ends' => 56,
'interval' => 4,
@init90
init90 / gist:50699bc25b27abb3f87fb5d0d7ff170c
Last active May 5, 2018 21:11
drupal 7, change entity paths.
/**
* Implements hook_entity_info_alter().
*/
function cba_events_entity_info_alter(&$entity_info) {
// Update exam events path.
$entity_info['event']['bundles']['exam_event']['crud']['edit']['path'] = 'event/exam/%eckentity/edit';
$entity_info['event']['bundles']['exam_event']['crud']['delete']['path'] = 'event/exam/%eckentity/delete';
// Update default event path.
$entity_info['event']['bundles']['default']['crud']['add']['path'] = 'event/default/add';
@init90
init90 / cba_events.module
Created December 28, 2016 08:29
drupal 7, display add entity form in ctools popup.
/**
* Create default entity in ctools popup.
*/
function cba_events_create_default_entity_in_popup($js, $user_id) {
if (!$js) {
return drupal_goto('event/default/add');
}
ctools_include('ajax');
ctools_include('modal');
@init90
init90 / cba_communication.manual_run.inc
Created December 29, 2016 16:42
Drupal 7, display status message in ctools popup
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
drupal_set_message('test');
ctools_modal_render('test', theme('status_messages'));
@init90
init90 / gist:3a4488a7036294db4aca335e6288e5f9
Last active May 5, 2018 21:06
drupal 7, entity query by date and entity reference field.
$q = new EntityFieldQuery;
$exam_id = $exam_wrp->field_event_exan_id->value();
$assignee_user = $q->entityCondition('entity_type', 'event')
->propertyCondition('type', 'exam_event')
->fieldCondition('field_start_event_date', 'value', date('Y-m-d'), '>')
->fieldCondition('field_event_exan_id', 'value', $exam_id, '=')
->fieldCondition('field_assignee', 'target_id', array($GLOBALS['user']->uid), 'IN')
->execute();
@init90
init90 / scratch.txt
Last active May 5, 2018 21:03
drupal 7, remove one value from multiple entity reference field.
$exam_wrp = entity_metadata_wrapper('event', 10);
$exam_wrp->field_assignee[ITEM_KEY]->set();
$exam_wrp->save();