Skip to content

Instantly share code, notes, and snippets.

View nickopris's full-sized avatar

Nick Opris nickopris

  • London, UK
View GitHub Profile
@nickopris
nickopris / db_query_result_to_csv
Created April 7, 2014 10:20
From db_query result to csv in Drupal
// Add Headers
drupal_add_http_header('Content-Type', 'text/csv');
drupal_add_http_header('Content-Disposition', 'attachment;filename=name_your_file.csv');
$fp = fopen('php://output', 'w');
$first = TRUE;
foreach ($results as $line) {
//Gets the line so we can flip it and get the column names
$column_names = get_object_vars($line);
@nickopris
nickopris / add_taxonomy_d7.php
Created May 16, 2014 09:40
Drupal 7 taxonomy vocabulary, terms and fields
<?php
// Create new vocabulary
$vo = new stdClass();
$vo->name = "AAA";
$vo->machine_name = "aaa";
taxonomy_vocabulary_save($vo);
// Create field in db
if(!db_table_exists('field_data_field_id_on_hub')) {
$field = array(
@nickopris
nickopris / commerce_new_profile
Created June 6, 2014 10:11
Commerce new profile
$type = 'billing';
$new_profile = commerce_customer_profile_new($type);
$address[] = array(
'country' => 'GB',
'administrative_area' => 'county',
'sub_administrative_area' => '',
'locality' => 'London',
'dependent_locality' => '',
'postal_code' => 'XX 111',
@nickopris
nickopris / Drupal impersonate
Last active August 29, 2015 14:03
Allow to impersonate as an user account based on information from a content type.
/**
* Allow Masquerade role users to impersonate Role1 or Role2 accounts.
* Note that these users will never see their own account for editing or otherwise.
* As soon as they log in they will act on behalf of the account
* that we specify they can masquerade as.
*
* Only administrators should be able to control the list of users
* allowed to masquerade.
*
* @param $edit
insurec.services.yml
services:
insurec.event_subscriber:
class: Drupal\insurec\EventSubscriber\InsurecSubscriber
tags:
- { name: 'event_subscriber' }
insurec/src/EventSubscriber/InsurecSubscriber.php
@nickopris
nickopris / haversine.php
Created March 6, 2021 09:08
Geolocation distance between two points with lat and lon values
<?php
/**
* https://www.movable-type.co.uk/scripts/latlong.html
* @param $lat1
* @param $lat2
* @param $lon1
* @param $lon2
*
* @return float|int
@nickopris
nickopris / geoLocationFromPostcode.php
Created March 6, 2021 09:11
Postcodes.io geolocation search by postcode
<?php
/**
* @param $postcode
*
* @return array
*/
function getGeoLocationFromPostcode($postcode) {
$geo = [];
$url = "https://api.postcodes.io/postcodes/" . $postcode;
<?php
$dates = [
"12/01/2021",
"12/05/2021",
"12/09/2021",
"12/10/2021",
"12/11/2021",
"12/15/2021",
"12/10/2021",
@nickopris
nickopris / shoutcast.php
Created April 7, 2022 11:58 — forked from njh/shoutcast.php
PHP script to check the status of a Shoutcast stream
<?php
$PLAYLIST_URL = 'http://www.bbc.co.uk/radio/listen/live/r1.pls';
set_time_limit(5);
// Fetch and parse a .pls playlist file
// @returns an array of URLs in the playlist
function readPlaylist($url)
<?php
/**
* @file Contains \Drupal\custom_module\Plugin\Field\FieldType\RoleRate
*/
namespace Drupal\custom_module\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;