Skip to content

Instantly share code, notes, and snippets.

@mrded
mrded / example.php
Last active December 26, 2015 18:19
PHP: ArrayObject examples.
<?php
$foo = isset($array['foo']) ? $array['foo'] : null;
$bar = isset($array['bar']) ? $array['bar'] : null;
# With php notices
$foo = $array['foo'] ? : null;
$bar = $array['bar'] ? : null;
# DefaultingArrayObject
@mrded
mrded / module-name.js
Created October 28, 2013 13:03
Drupal agreement: Working with JS variables.
(function ($) {
Drupal.behaviors.moduleName = {
attach: function(context, settings) {
console.log(settings.module_name.foo); // bar
}
};
})(jQuery);
@mrded
mrded / foo.module
Created October 31, 2013 13:46
Drupal: Taxonomy terms as autocomplete using form API.
<?php
function bar_form($form, &$state) {
$form['bar'] = array(
'#type' => 'textfield',
'#title' => t('Bar'),
'#autocomplete_path' => 'taxonomy/autocomplete/field_bar_term', // Field name here.
'#element_validate' => array('foo_taxonomy_autocomplete_validate'),
);
@mrded
mrded / ubuntu-vpn-install.sh
Last active December 27, 2015 02:49 — forked from nshkuro/setup vpn script
Ubuntu: Install and setup VPN (poptop)
#!/bin/bash
echo "Select on option:"
echo "1) Set up new PoPToP server AND create one user"
echo "2) Create additional users"
read x
if test $x -eq 1; then
echo "Enter username that you want to create (eg. client1 or john):"
read u
echo "Specify password that you want the server to use:"
read p
@mrded
mrded / foo.js
Created November 5, 2013 10:36
Drupal: Update item by ajax.
(function($) {
Drupal.behaviors.Foo = {
attach:function (context, settings) {
// unbind() because click events firing multiple times.
$('#foo a.update', context).unbind().click(function (event) {
// Get parrent of item.
var item = $(this).closest(".item");
// Get some data of item.
@mrded
mrded / get_links_and_change.php
Created November 7, 2013 13:10
Drupal: Get links from HTML dom and change them.
@mrded
mrded / foo.module
Created November 11, 2013 19:22
Drupal 8: Working with variables.
<?php
\Drupal::config('module_name.settings')->set('variable', 'hello');
$variable = \Drupal::config('module_name.settings')->get('variable');
@mrded
mrded / boost_solr.php
Created November 15, 2013 15:46
Drupal: boost relevance for solr search. #1926090
<?php
/**
* Implements hook_search_api_solr_query_alter().
*/
function custom_api_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface &$query) {
$call_args['params']['fl'] = 'is_votes-sightsrank,' . $call_args['params']['fl'] ;
$call_args['params']['bf'] = "recip(rord(is_votes-sightsrank),1, 1000, 1000)";
}
@mrded
mrded / custom_index.php
Created November 20, 2013 17:01
Drupal: Add custom field to Search API Solr index
<?php
/**
* Implements hook_entity_property_info_alter().
*/
function example_search_api_property_entity_property_info_alter(&$info) {
$info['node']['properties']['created_week_day'] = array(
'type' => 'text',
'label' => t('Week day of node creation'),
'sanitized' => TRUE,
'getter callback' => 'example_search_api_property_weekday_getter_callback',
@mrded
mrded / foo.rb
Created November 20, 2013 21:09
Ruby: Get array of prime numbers.
(1..100).select {|x| (1..x).select{|y|x%y==0}.size==2 }
=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]