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 / blocktube_advanced_blocking.js
Last active March 16, 2022 09:34
Provides a function to turn Youtube into a whitelist-only mode. This function depends on the BlockTube extension for Chrome/Firefox and should be used in its "Advanced blocking" option. The example values used below will only allow videos coming from the "Om Nom
/**
* The following function will make BlockTube act in a whitelist-only mode.
*
* Any videos coming from channels other that those you have set in the
* "allowedChannels" variable will get hidden from display.
*/
(video, objectType) => {
/*
* Add here the channel IDs you want to be displayed.
*/
@vensires
vensires / template.php
Created April 2, 2018 15:04
The following code is useful when using links with the .colorbox-load class following the pattern "http://www.example.com/path?width=500&height=500&iframe=true". Only the page content region is displayed and everything else gets hidden
<?php
/**
* Implements hook_process_page().
*/
function MYTHEME_process_page(&$variables) {
if (!empty($_GET['iframe'])) {
foreach (element_children($variables['page']) as $index) {
if ($index == 'content') {
continue;
}
@vensires
vensires / gdImage_manipulator.php
Created March 23, 2018 01:38
The following script takes the path of an image and a color value to set to Alpha. Then the image takes as background the $bgcolor. A final parameter $mirror is used to mirror the image horizontally.
<?php
/*
* The callback function to process the image.
*
* @param string $filepath
* The file to the path.
* @param string $color
* The HEX value of the color.
* @param string $bgcolor
@vensires
vensires / drupal_allow_protected_folders.htaccess
Created February 2, 2018 12:06
In a Drupal installation on a cPanel server, there was a problem with some protected directories. Instead of displaying the autorization popup, Drupal's 404 page was invoked. It seems Apache displays the 404 page if a 401 page does not exist. The solution was found in https://stackoverflow.com/a/22980537. Instead of creating a real HTML page for…
#
# Apache/PHP/Drupal settings:
#
# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)(~|\.sw[op]|\.bak|\.orig|\.save)?$|^(\.(?!well-known).*|Entries.*|Repository|Root|Tag|Template|composer\.(json|lock))$|^#.*#$|\.php(~|\.sw[op]|\.bak|\.orig\.save)$">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
@vensires
vensires / slick.add_class_to_cloned.js
Created January 29, 2018 12:24
Slick slider adds some .slick-cloned divs before and after the original slide content. These divs don't get the slick-active class but for theming purposes this might be required. The following code adds the .slick-cloned-active class to .slick-cloned elements when active.
(function($) {
// Initialize slick slider on elements having the .slick class.
var slider = $('.slick');
slider.slick({
// There is no reason for cloned divs to appear when infinite is set to false.
infinite: true
});
slider.on('beforeChange', function(event, slick, currentSlide, targetSlide) {
var $targetSlide = $(slick.$slides[targetSlide]),
@vensires
vensires / drupal_array_get_nested_key_parents.php
Created January 10, 2018 14:31
The following function returns the hierarchy of the first-found instance of a key an array. The concept was to take a renderable array, and build the $parents array as required by drupal_array_get_nested_value.
<?php
/**
* Returns the hierarchy of the first found instance of a key in an array.
*/
function drupal_array_get_nested_key_parents($needle, $haystack, &$parents = array()) {
foreach ($haystack as $key => $value) {
if ((string) $key == (string) $needle) {
$parents[] = $key;
return TRUE;
}
@vensires
vensires / drupal.commerce.checkout.js
Created January 10, 2018 12:04
Contains various general fixes for the Drupal Commerce checkout page. I usually attach the JS files in the commerce checkout page using a single-page checkout.
(function($) {
/*
* Disable the Checkout Continue button when an AJAX request is pending but
* reenable it when all AJAX requests have ended.
*/
Drupal.behaviors.commerceOverridesCheckoutContinueButton = {
attach: function (context, settings) {
var submitButton = $('body.page-checkout-checkout form[id^="commerce-checkout-form-checkout"] input.checkout-continue');
if (submitButton.length) {
// Disable the submit button when an AJAX request starts.
@vensires
vensires / drupalExecuteFromCLI.php
Created December 7, 2017 09:34
For anyone trying to use Drupal code in command line and having issues, use the following code as an example. When using Drupal in CLI, a warning about "Undefined index: REMOTE_ADDR" will appear for bootstrap.inc file. The following code fixes this.
<?php
/**
* @file
* The following code is retrieved from a comment in drupal.org/forum with
* minor modifications, mostly in comments.
*
* @see https://www.drupal.org/forum/support/module-development-and-code-questions/2010-12-29/drupal-7-and-bootstrapinc-problem#comment-4327272
*/
$uid = 1;
// Prevent this from running under Apache.
@vensires
vensires / drupalModuleLibraryExists.php
Last active December 6, 2017 09:46
Find if a library exists in a Drupal path recognisable by Libraries module without needing libraries_load() or an implementation of hook_libraries_info(). This code is looking for the MPDF library.
<?php
/**
* Function to check existence of mPDF library.
*
* @return bool
* TRUE if mPDF library path exists and FALSE if it isn't.
*/
function module_mpdf_exists() {
// Search for mpdf tool first.
$pattern = '/^mpdf.php$/';
@vensires
vensires / drupalRenderBlockWithTitle.php
Created October 26, 2017 13:56
Render a block in Drupal 7 anywhere in the page (eg. in a tpl.php file) with its title.
<?php
/**
* Render a block anywhere with its title.
*
* To render the block of the "ep_socialchannels" module with the
* "social_channels" delta, call this function, like this:
* @code
* THEME_render_block('ep_socialchannels', 'social_channels');
* @encode
*