Skip to content

Instantly share code, notes, and snippets.

@mrded
mrded / create_field_collection_item.php
Last active December 24, 2015 16:09
Drupal: Example of creating field_collection item.
<?php
$entity = entity_create('field_collection_item', array('field_name' => 'field_education'));
$entity->profile_university_term[LANGUAGE_NONE][]['tid'] = _wj_users_reg_add_tag('university', $values);
$entity->profile_degree_term[LANGUAGE_NONE][]['tid'] = _wj_users_reg_add_tag('degree', $values);
$entity->profile_first_year[LANGUAGE_NONE][]['value'] = format_date($first_year_time, 'custom', 'Y-m-d H:i:s');
$entity->profile_graduation_year[LANGUAGE_NONE][]['value'] = format_date($graduation_year_time, 'custom', 'Y-m-d H:i:s');
$entity->setHostEntity('profile2', $profile);
$entity->save();
@mrded
mrded / codes_controller.rb
Last active December 25, 2015 03:29
Rails snippet
# rails generate controller Codes index show new create edit update destroy
class CodesController < ApplicationController
def index
@codes = Code.all
end
def show
@code = Code.find params[:id]
end
@mrded
mrded / split_entity_templates.php
Created October 10, 2013 16:43
Split templates for entity by view_mode
<?php
/**
* Implements hook_preprocess_entity().
*/
function wj_profile_preprocess_entity(&$vars) {
switch ($vars['entity_type']) {
case 'profile2':
$vars['theme_hook_suggestions'][] = "profile2__profile__{$vars['view_mode']}";
$function_name = "wj_profile_preprocess_profile2_profile_{$vars['view_mode']}";
if (function_exists($function_name)) {
@mrded
mrded / codes_controller.rb
Last active December 25, 2015 15:19
Rails: How to render different format.
class CodesController < ApplicationController
def index
@codes = Code.all
respond_to do |format|
format.html
format.json do
render json: @codes,
only: [:id, :name, :description]
end
@mrded
mrded / invoke_hook.php
Last active December 25, 2015 19:29
Drupal: example of hook invoking
<?php
$baz = $qux = t('Arguments');
$bar = array();
foreach (module_implements('foo') as $module) {
$bar[] = module_invoke($module, 'foo', $baz, $qux);
}
# module_invoke_all('foo', $baz, $qux);
@mrded
mrded / code.rb
Created October 20, 2013 23:53
Rails: Add virtual attribute to model.
class Code < ActiveRecord::Base
attr_accessor :value
def value
'hello'
end
end
@mrded
mrded / foo.module
Created October 22, 2013 16:45
Drupal: custom entity view modes.
<?php
/**
* Implements hook_entity_info_alter().
*/
function foo_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['another_teaser'] = array(
'label' => t('Another teaser'),
'custom settings' => TRUE,
);
@mrded
mrded / codes.json
Last active December 26, 2015 06:49
AngularJS: Search by JSON.
[{"id":1,"name":"X99.7","description":"Not known","value":"X99.7"},{"id":2,"name":"X99.8","description":"Outpatient procedure carried out but no appropriate OPCS-4 code available","value":"X99.8"},{"id":3,"name":"X99.9","description":"No outpatient procedure carried out","value":"X99.9"},{"id":4,"name":"A01.3","description":"Partial lobectomy of brain","value":"A01.3"},{"id":5,"name":"A01.8","description":"Other specified major excision of tissue of brain","value":"A01.8"},{"id":6,"name":"A01.9","description":"Unspecified major excision of tissue of brain","value":"A01.9"},{"id":7,"name":"A02.4","description":"Excision of lesion of tissue of occipital lobe of brain","value":"A02.4"},{"id":8,"name":"A02.5","description":"Excision of lesion of tissue of cerebellum","value":"A02.5"},{"id":9,"name":"A03.4","description":"Stereotactic ablation of tissue of brain stem","value":"A03.4"},{"id":10,"name":"A03.9","description":"Unspecified stereotactic ablation of tissue of brain","value":"A03.9"},{"id":11,"name":"A04.
@mrded
mrded / ctools_popup.php
Last active September 19, 2017 13:21
Drupal: Example of reloading ctools modals.
<?php
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
drupal_add_library('module', 'fancybox.modal'); // Include js here.
$path = 'some_path/nojs';
$content = ctools_modal_text_button($text, $path, '', $class);
@mrded
mrded / list_of_users.rules.inc
Created October 25, 2013 08:33
Drupal: How to pass list of users to bloody rules.
<?php
/**
* Implements hook_rules_event_info().
*/
function hook_rules_event_info() {
return array(
'list_of_users' => array(
'label' => t('List of users example'),
'group' => t('Example'),
'variables' => array(