Skip to content

Instantly share code, notes, and snippets.

View vensires's full-sized avatar

Panagiotis Moutsopoulos vensires

  • E-sepia
  • Athens, Greece
View GitHub Profile
@vensires
vensires / drupalFixMailchimpBlockSignupFormRedirect.module.php
Created October 11, 2017 18:43
Place the following code in a custom Drupal (7.x) module to fix the issue described in https://www.drupal.org/node/2915545. Replace MODULE with your custom module's name.
<?php
/**
* Implements hook_form_mailchimp_signup_subscribe_form_alter().
*/
function MODULE_form_mailchimp_signup_subscribe_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'MODULE_mailchimp_signup_subscribe_form_redirect';
}
/**
* Submit callback for mailchimp signup subscribe form.
@vensires
vensires / darkenlight.php
Created October 10, 2017 08:07
Turns a HEX color to a darker/lighter shade. If your needs are more intensive and you require more that just a function, you could check the phpColors repo: https://github.com/mexitek/phpColors
<?php
/**
* Turns a HEX color to a darker/lighter shade.
*
* @param string $colorstr
* The color as a HEX string with a # prefix.
* @param int $steps
* The number of steps to alter the color to. Both negavive and positive
* numbers are allowed.
*
@vensires
vensires / sayEuro.php
Last active October 6, 2017 08:51
A PHP class to turn an amount from EUR to Greek text.
<?php
/**
* Class Euro2Text.
*
* Translates an amount in EUR to Greek text. Only the main sayEuro() method
* is public. Every other method is set to protected in order to allow
* developers to extend this class for their own purposes.
*
* Original Credit for the Greek version goes to Uberalles_gr
@vensires
vensires / getGoogleAnalytics.jquery.js
Last active October 3, 2018 15:09
JS script to be executed in console.log() in order to find the Google Analytics UA code of a website. The same code code be used in a bookmarklet.
/*
* The following script requires jQuery to exist in the website.
*/
(function($) {
if (typeof(ga) == "undefined") {
console.log('No Google Analytics script found');
}
else {
var UAs = ga.getAll();
$.each(UAs, function(key, value) {
@vensires
vensires / request_forms.info
Created June 26, 2017 10:12
A custom module to use as a base for a request form. It also contains pathauto implementation so that
name = Request Forms
core = 7.x
description = Creates the request forms of the website
dependencies[] = pathauto
@vensires
vensires / imageprefillalttitle.php
Last active February 21, 2017 15:04
When an image field displays an image with empty *alt* or *title* attributes, these get filled with the label of the corresponding entity. Using hook_preprocess_field() we target view mode displays but not Views using fields. hook_field_attach_view_alter() though targets everything. This code may play well in both a module and a theme.
<?php
/**
* Implements hook_field_attach_view_alter().
*
* It fills the empty "alt" and "title" attributes of images with the label of
* the corresponding entity.
*/
function mymodule_field_attach_view_alter(&$result, $context) {
if (!empty($context['entity_type']) && !empty($context['entity'])) {
$label = entity_label($context['entity_type'], $context['entity']);
@vensires
vensires / detect_faulty_variables.php
Created January 3, 2017 13:24
A small script to help you fix the notice with a Drupal variable not being able to be properly unserialized. Make sure you have the devel module enabled for the dpm function used in the error handler.
<?php
set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) {
dpm($errcontext);
});
$variables = db_query('SELECT name, value FROM {variable}')->fetchAllKeyed();
foreach($variables as $name => $values) {
$value = unserialize($values);
}
restore_error_handler();
@vensires
vensires / product_weights_to_packages.php
Created December 19, 2016 18:29
Divides an array of different products weights to packages containing at most $package_max_weight weight and $package_max_items items. You could use this function to calculate shipping based on package weight instead of product weight.
<?php
/**
* Divides product weights into packages.
*
* @param array $weights
* An array of product weights.
* @param int $package_max_weight
* The maximum weight of a package. Set to 0 to not allow limit per weight.
* @param int $package_max_items
* The maximum number of items per package. Set to 0 to not allow limit per items.
@vensires
vensires / php2delphiTDate.php
Created November 3, 2016 15:28
PHP function to turn a DateTime object to Delphi's TDate type. Delphi's TDate type is something like UnixTimestamp; it is a (float) number but it has as a starting poing the 1899-12-30 instead of the 1970-01-01 as is the case in the UnixTimestamp.
<?php
/**
* For the sake of example, I assume that the caller only needs the date without any time information.
* If you also need the time, you will have to modify the following code a little.
* @param object $phpDate
* A DateTime PHP object
* @return int
* The number of days from 1899-12-30 (Delphi's starting point). This is also the correct value to be
* stored as a TDate type variable in Delphi.
*/
@vensires
vensires / scenario1.php
Created August 2, 2016 10:31
PHP: Memory consumption in different scenarios by counting memory used by PHP but not allocated to PHP
<?php
/**
* The following code uses 735992 bytes = 735.99200 kilobytes
*/
print memory_get_usage(FALSE);
$limit = 1;
for($i = 0; $i < $limit; $i++) {
$func = 'hello_' . $i;
$$func = function() {
};