Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created November 8, 2012 05:12
Show Gist options
  • Select an option

  • Save carlwiedemann/4036948 to your computer and use it in GitHub Desktop.

Select an option

Save carlwiedemann/4036948 to your computer and use it in GitHub Desktop.
Markup utility functions sandbox...looking for a proof-of-concept
<?php
/**
* Refactored l() and theme-less version of theme_link().
*
* @see markup_html_tag().
*/
function markup_l($title, $path, $options = array()) {
// Merge in defaults.
$options += array(
'attributes' => array(),
'html' => FALSE,
);
// Append active class.
if (($path == current_path() || ($path == '<front>' && drupal_is_front_page())) &&
(empty($options['language']) || $options['language']->langcode == language(LANGUAGE_TYPE_URL)->langcode)) {
$options['attributes']['class'][] = 'active';
}
// Remove all HTML and PHP tags from a tooltip. For best performance, we act only
// if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
$options['attributes']['title'] = strip_tags($options['attributes']['title']);
}
// Set href attribute.
$options['attributes']['href'] = check_plain(url($path, $options));
return _markup_html_tag('a', ($options['html'] ? $text : check_plain($text)), $options['attributes']);
}
/**
* Theme-less version of theme_image().
* @param $uri
* Drupal file URI for the image to be changed to src attribute.
* @return An <img> tag string.
* @see markup_html_tag().
*/
function markup_image($uri, $attributes = array()) {
$attributes['src'] = file_create_url($uri);
return _markup_html_tag('img', NULL, $attributes);
}
/**
* Theme-less version of theme_image_style().
*/
function markup_image_style($style_name, $uri, $attributes = array()) {
// Determine the dimensions of the styled image.
$dimensions = array(
'width' => !empty($attributes['width']) ? $attributes['width'] : NULL,
'height' => !empty($attributes['height']) ? $attributes['height'] : NULL,
);
image_style_transform_dimensions($style_name, $dimensions);
// ======================================================
// = Problem: what about the class that was added here? =
// ======================================================
// Determine the URL for the styled image.
$attributes['src'] = image_style_url($style_name, $uri);
return _markup_html_tag('img', NULL, $attributes);
}
/**
* Theme-less HTML tag builder helper. This isn't designed to be called directly.
* @param $tag
* The name of the tag.
* @param $value
* The inner value of the tag.
* @param $attributes
* Array of HTML attributes to be built as Attribute object.
* @param $cdata
* Whether to wrap the tag inner value with CDATA parameters.
* @return Built HTML tag.
*/
function _markup_html_tag($tag, $value = NULL, $attributes = array(), $cdata = FALSE) {
// ==================================================================
// = Problem: overengineering with something like hook_tag_alter(). =
// ==================================================================
// $args = func_get_args();
// drupal_alter('tag', $args);
// list($tag, $value, $attributes, $cdata) = $args;
$attributes = !empty($attributes) ? new Attribute($attributes) : '';
if (isset($value)) {
// Open tag.
$output = '<' . $tag . $attributes . '>';
// Consider CDATA.
$output .= $cdata ? "\n<!--/*--><![CDATA[/*><!--*/\n" . $value . "\n/*]]>*/-->\n" : $value;
// Close tag.
$output .= '</' . $element['#tag'] . ">\n";
return $output;
}
else {
// No inner value.
return '<' . $tag . $attributes . " />\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment