Skip to content

Instantly share code, notes, and snippets.

View gh640's full-sized avatar

Goto Hayato gh640

View GitHub Profile
@gh640
gh640 / show-git-status-in-termnal-prompt.sh
Created February 6, 2016 08:14
Bash PS1 for Git repository
# Show Git branch name and number of changed files
# if current directory is in Git repo.
# カレントディレクトリが Git リポジトリの場合に
# 現在のブランチと変更ファイルの数を表示する形にコマンドプロンプトをカスタマイズする
if [ $TERM == xterm ]; then
PS1='\[$(tput setaf 4)\]\u:\W\$\[$(tput sgr0)\]$(if git status &>/dev/null;then echo \[$(tput setaf 5)\][$(git branch | grep ^\* | cut -d " " -f 2) changed:$(git status -s | wc -l)]\[$(tput sgr0)\];fi) '
fi
@gh640
gh640 / drupal-coding-standard-customization-for-Japanese.md
Created February 6, 2016 08:16
PHP Code Sniffer を日本語でも快適に使うための Drupal コーディングスタンダードファイルの修正箇所

LineLengthSniff.php:

// changed for Japanese text.
// public $lineLimit = 80;
public $lineLimit = 500;

FunctionCommentSniff.php:

// comment out for Japanese comment.

// if (preg_match('|[A-Z]|', $testShort[0]) === 0) {

@gh640
gh640 / drupal-7-filter-for-showing-blocks-only-in-user-profile-pages.php
Created February 6, 2016 08:20
Drupal 7 で特定のブロックをユーザの表示ページのみに表示するための PHP フィルタコード
<?php
// ユーザの表示ページ、かつ、そのユーザが admin 以外なら表示する
$matches = array();
$result = preg_match('/^user(\/(\d+))?$/', request_path(), $matches);
if ($result && (count($matches) == 1 || $matches[2] != 1)) {
return TRUE;
}
else {
@gh640
gh640 / drupal-7-has-user-role.php
Created February 6, 2016 08:24
Drupal 7 でユーザがロールを持つかどうかを調べる関数
<?php
/**
* ユーザが特定のロールを持つかどうかを調べる
*
* @param object $account
* チェック対象ユーザのオブジェクト。
* @param array|int $roles
* ロールの文字列の配列。インデックス配列。
@gh640
gh640 / how-to-use-chromephp-in-drupal.md
Created February 6, 2016 08:27
Drupal 7 での ChromePHP の使い方

Drupal での ChromePHP の使い方

1. セットアップ

  1. ChromeLogger をインストール
  1. ChromePhp.php ダウンロードして PHP include パスのどこかに入れる
@gh640
gh640 / drupal-7-get-parent-menu-path.php
Last active August 14, 2022 02:34
Drupal 7 で現在のページのメニュー上の親のページのパスを取得する
<?php
// 現在のページが `user/1/cards/create` なら
// $parent_menu_item_path は `user/1/cards` になる
$current_menu_item = menu_get_item();
$parent_menu_item_path = $current_menu_item['tab_parent_href'];
@gh640
gh640 / drupal-7-generate-new-hash-salt.md
Last active February 6, 2016 08:38
Drupal 7 でターミナルから新しいハッシュソルトを生成する方法

With drush:

drush php-eval 'echo(drupal_random_key()) . PHP_EOL;'

With openssl:

openssl rand -base64 32
@gh640
gh640 / convert_timestamp_to_date.py
Created February 6, 2016 08:40
UNIX タイムスタンプを Y-m-d 形式の日付に変換するコマンドスクリプト
#!python3
# coding: utf-8
"""
usage: python convert_timestamp_to_date.py [timestamp]
If you add alias for this, it's much easier to use this.
"""
@gh640
gh640 / hatena-get-bookmark-count.sh
Created February 6, 2016 12:43
特定のサイトやページのはてブ数を取得するターミナル関数
# サイト全体のはてブ数を取得する
# get the sum total of hatena bookmark count for a site
#
# usage:
# hatena_get_count_for_site http://example.com
#
function hatena_get_count_for_site {
# @see http://developer.hatena.ne.jp/ja/documents/bookmark/apis/getcount
if [ "$#" -ne 1 ]; then
@gh640
gh640 / generate_hours_in_30min_interval.py
Created February 6, 2016 12:45
1 日の時間を 30 分刻みですべて生成するスクリプト
# coding: utf-8
# 1 日の 30 分刻みの時刻をすべて生成する
# 00:00, 00:30, 01:30, ..., 23:30
hours = []
for h in range(24):
for m in (0, 30):
hours.append('{0:02d}:{1:02d}'.format(h, m))