Skip to content

Instantly share code, notes, and snippets.

View henrytran9x's full-sized avatar

Henry Tran henrytran9x

View GitHub Profile
@henrytran9x
henrytran9x / video.php
Created March 1, 2015 15:36
Check & Parse YouTube & Vimeo Video IDs
<?php
/*
* Check if URL is YouTube
*/
function _is_youtube($url)
{
return (preg_match('/youtu\.be/i', $url) || preg_match('/youtube\.com\/watch/i', $url));
}
@henrytran9x
henrytran9x / view-display-commissons
Created March 2, 2015 09:11
Displaying Commissions of user by module commrece affiliate
$view = new view();
$view->name = 'user_s_comissions';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_affiliate_commission';
$view->human_name = 'User\'s Comissions';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
@henrytran9x
henrytran9x / mymodule.php
Last active August 29, 2015 14:16
Show amount of results for exposed filter Drupal 7
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch($form_id){
case 'views_exposed_form':
foreach ($form['field_marka_prochnosti_tid']['#options'] as $tid => &$value) {
$query = db_select('field_data_field_marka_prochnosti', 'f')
->condition('f.field_marka_prochnosti_tid', $tid);
$query->addExpression('COUNT(*)');
$count = $query->execute()->fetchField();
$value = $value . ' (' . $count . ')';
@henrytran9x
henrytran9x / theme_drupal
Created March 6, 2015 07:28
Learning Drupal Theme
/* Working with template suggestions */
https://www.drupal.org/node/1089656
/* Overview of theme files */
https://www.drupal.org/node/171194
@henrytran9x
henrytran9x / contextual_demo.php
Last active August 29, 2015 14:17
Add custom Contextual Links to Drupal node teaser pages
<?php
/**
* Implements hook_permission().
*/
function YOUR_MODULE_permission() {
return array(
'YOUR MODULE article unpublish articles' => array(
'title' => t('YOUR MODULE Article Unpublish'),
),
);
@henrytran9x
henrytran9x / render_view.php
Last active August 29, 2015 14:17
Programmatically render views in Drupal 7
<?php
print views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID', $view_arg);
$view = views_get_view('VIEWS_MACHINE_NAME');
$view->set_display('DISPLAY_ID');
print $view->get_title();
print $view->preview('DISPLAY_ID');
?>
@henrytran9x
henrytran9x / demo_module.php
Created March 13, 2015 17:07
Creating views view in your module is really simple.
<?php
// 1.
// Create your view the regular way.
// 2.
// Define your views api version and path for files that include views hooks.
/**
* Implementation of hook_views_api().
*/
function HOOK_views_api() {
@henrytran9x
henrytran9x / mymodule.php
Created March 14, 2015 02:16
Using views with a custom table/schema
<?php
/*
CREATE TABLE example_table (
nid INT(11) NOT NULL,
plain_text_field VARCHAR(32,
numeric_field INT(11),
boolean_field INT(1),
timestamp_field INT(8),
PRIMARY KEY(nid)
@henrytran9x
henrytran9x / mymodule.php
Created March 27, 2015 02:22
Write module used hook_cron process delele node content type !
function MYMODULE_cron() {
$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'YOUR-CONTENT-TYPE')
->execute();
if (isset($result['node']) && count($result['node'])) {
$node_ids = array_keys($result['node']);
@henrytran9x
henrytran9x / template.php
Created May 3, 2015 04:58
theme_hook_suggestions for taxonomy page Drupal 7
function mytheme_preprocess_page(&$variables) {
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
//kiểm tra + thêm điều kiện khi mình muốn load suggestion cho term hoặc vocalbulary nào
$variables['theme_hook_suggestions'][] = 'page__taxonomy_term';
}
}
// Vào thư mục 'templates' tạo file page--taxnomy_term.tpl.php ..overwrite page