Skip to content

Commit

Permalink
Issue backdrop#364: PoC API for icons.
Browse files Browse the repository at this point in the history
  • Loading branch information
quicksketch committed Apr 7, 2024
1 parent 336f333 commit df47d52
Show file tree
Hide file tree
Showing 44 changed files with 694 additions and 275 deletions.
6 changes: 6 additions & 0 deletions .cspell/backdrop.dic
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ pagenum
pagerpieces
phptemplate
phpwiki
plid
plids
pneum
Ponedjeljak
Expand All @@ -342,6 +343,9 @@ queryor
quicktime
Quieligo
ratis
rawurldecode
rawurlencode
rawurlencoded
readwrite
realelement
refclass
Expand Down Expand Up @@ -454,6 +458,7 @@ unmunged
unmunging
unnested
unpackage
unrendered
uploadimage
uploadwidget
USERAGENT
Expand All @@ -467,6 +472,7 @@ VERIFYHOST
VERIFYPEER
verygreatbackdropmodule
VIEWNAME
viewmode
watchdog
WCAG
wday
Expand Down
53 changes: 52 additions & 1 deletion core/includes/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5075,6 +5075,7 @@ function backdrop_aggregate_js(&$js_groups) {
* to elements using the #attached property. The #attached property is an
* associative array, where the keys are the the attachment types and the values
* are the attached data. For example:
*
* @code
* $build['#attached'] = array(
* 'js' => array(backdrop_get_path('module', 'taxonomy') . '/js/taxonomy.admin.js'),
Expand Down Expand Up @@ -5122,6 +5123,7 @@ function backdrop_aggregate_js(&$js_groups) {
* @see backdrop_add_library()
* @see backdrop_add_js()
* @see backdrop_add_css()
* @see backdrop_add_icon()
* @see backdrop_render()
*/
function backdrop_process_attached($elements, $group = JS_DEFAULT, $dependency_check = FALSE, $every_page = NULL) {
Expand All @@ -5130,6 +5132,7 @@ function backdrop_process_attached($elements, $group = JS_DEFAULT, $dependency_c
'library' => array(),
'js' => array(),
'css' => array(),
'icons' => array(),
);

// Add the libraries first.
Expand Down Expand Up @@ -5175,6 +5178,10 @@ function backdrop_process_attached($elements, $group = JS_DEFAULT, $dependency_c
unset($elements['#attached'][$type]);
}

// Icons are added all at once, no need to apply separate options.
backdrop_add_icons($elements['#attached']['icons']);
unset($elements['#attached']['icons']);

// Add additional types of attachments specified in the render() structure.
// Libraries, JavaScript and CSS have been added already, as they require
// special handling.
Expand Down Expand Up @@ -5396,6 +5403,7 @@ function backdrop_add_library($module, $name, $every_page = NULL) {
'library' => $library['dependencies'],
'js' => $library['js'],
'css' => $library['css'],
'icons' => $library['icons'],
);
$added[$module][$name] = backdrop_process_attached($elements, JS_LIBRARY, TRUE, $every_page);
}
Expand Down Expand Up @@ -5454,7 +5462,12 @@ function backdrop_get_library($module, $name = NULL) {
foreach ($module_libraries as $key => $data) {
if (is_array($data)) {
// Add default elements to allow for easier processing.
$module_libraries[$key] += array('dependencies' => array(), 'js' => array(), 'css' => array());
$module_libraries[$key] += array(
'dependencies' => array(),
'js' => array(),
'css' => array(),
'icons' => array(),
);
foreach ($module_libraries[$key]['js'] as $file => $options) {
$module_libraries[$key]['js'][$file]['version'] = $module_libraries[$key]['version'];
}
Expand All @@ -5471,6 +5484,34 @@ function backdrop_get_library($module, $name = NULL) {
return $libraries[$module];
}

/**
* Adds icons to the page to make them available in JS and CSS files.
*
* The icon name is resolved to a file path and then added to the page as both
* as a JavaScript variable (Backdrop.icons['icon-name']) and as a CSS variable
* (--icon-[icon-name]). Note that use of this function is not necessary if
* embedding an icon directly onto the page using the icon() function. This is
* only needed if using icons in JS and CSS files.
*
* @param string[] $icon_names
* An array of unique icon names be added, without the extensions. Most icon
* names can be found by browsing the core/misc/icons directory.
*
* @see icon()
*/
function backdrop_add_icons(array $icon_names) {
$icon_paths = array();
foreach ($icon_names as $icon_name) {
if ($icon_path = icon_get_path($icon_name)) {
$icon_paths[$icon_name] = base_path() . $icon_path;
}
}
if ($icon_paths) {
backdrop_add_js(array('icons' => $icon_paths), 'setting');
backdrop_add_library('system', 'backdrop.icons');
}
}

/**
* Assists in adding the tableDrag JavaScript behavior to a themed table.
*
Expand Down Expand Up @@ -5985,6 +6026,7 @@ function _backdrop_bootstrap_full() {
require_once BACKDROP_ROOT . '/core/includes/tablesort.inc';
require_once BACKDROP_ROOT . '/core/includes/file.inc';
require_once BACKDROP_ROOT . '/core/includes/unicode.inc';
require_once BACKDROP_ROOT . '/core/includes/icon.inc';
require_once BACKDROP_ROOT . '/core/includes/image.inc';
require_once BACKDROP_ROOT . '/core/includes/form.inc';
require_once BACKDROP_ROOT . '/core/includes/mail.inc';
Expand Down Expand Up @@ -8049,6 +8091,15 @@ function backdrop_common_theme() {
'details' => array(
'render element' => 'element',
),
// From icon.inc.
'icon' => array(
'variables' => array(
'name' => NULL,
'path' => NULL,
'attributes' => array('class' => array()),
'wrapper_attributes' => array('class' => array()),
),
),
);
}

Expand Down
30 changes: 30 additions & 0 deletions core/includes/file.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,36 @@ function backdrop_move_uploaded_file($filename, $uri) {
return $result;
}

/**
* Get a file's extension such as .zip, .tar.gz, or .png.
*
* @param string $filename
* The file name or an entire path.
* @param integer $extension_count
* The maximum number of extensions to be returned.
*
* A value of 0, returns all file extensions. For example a file name of
* "example.foo.text.zip" would return "foo.text.zip". This may result in
* unexpected filenames as any period anywhere in the filename results in
* the remaining segments being consider an extension.
*
* The default value is 1 segment, where "example.foo.text.zip" would return
* simply "zip". A value of 2 would return "text.zip".
*/
function file_get_extension($filename, $extension_count = 1) {
$filename = basename($filename);
$extensions = explode('.', $filename);

// Remove the base filename.
array_shift($extensions);

// Reduce the extension list to the maximum count.
if ($extension_count !== 0 && $extension_count < count($extensions)) {
$extensions = array_slice($extensions, 0, $extension_count);
}
return implode('.', $extensions);
}

/**
* Checks that a file meets the criteria specified by the validators.
*
Expand Down
Loading

0 comments on commit df47d52

Please sign in to comment.