Skip to content

Instantly share code, notes, and snippets.

@mrkmiller
Last active April 1, 2019 17:45
Show Gist options
  • Select an option

  • Save mrkmiller/35adc71f5e8042c0950479b261a6f4bf to your computer and use it in GitHub Desktop.

Select an option

Save mrkmiller/35adc71f5e8042c0950479b261a6f4bf to your computer and use it in GitHub Desktop.
sitefarm seed brand color application in a theme.
article_category_field: field_sf_article_category
event_category_field: field_sf_event_type
photo_gallery_category_field: field_sf_gallery_category
branding_field: field_sf_branding
brand_color_field: field_sf_brand_color
<?php
/**
* Fetch the category brand color name for a given node
*
* @param $entity
* @return string - color name or bool FALSE if not set
*/
function sfseed_get_category_brand($entity) {
$article_category_field = theme_get_setting('article_category_field');
$event_category_field = theme_get_setting('event_category_field');
$photo_gallery_category_field = theme_get_setting('photo_gallery_category_field');
$branding_field = theme_get_setting('branding_field');
$brand_color_field = theme_get_setting('brand_color_field');
// Node.
if ($entity->getEntityTypeId() == 'node') {
// Articles.
if ($entity->$article_category_field && !$entity->$article_category_field->isEmpty()) {
// Grab the first category term.
return $entity->$article_category_field->entity->$brand_color_field->value;
}
// Events.
elseif ($entity->$event_category_field && !$entity->$event_category_field->isEmpty()) {
// Grab the first category term.
return $entity->$event_category_field->entity->$brand_color_field->value;
}
// Photo Galleries.
elseif ($entity->$photo_gallery_category_field && !$entity->$photo_gallery_category_field->isEmpty()) {
// Grab the first category term.
return $entity->$photo_gallery_category_field->entity->$brand_color_field->value;
}
// Nodes with the Branding Taxonomy.
elseif ($entity->$branding_field && !$entity->$branding_field->isEmpty()) {
// Grab the first category term.
return $entity->$branding_field->entity->$brand_color_field->value;
}
else {
return FALSE;
}
}
// Taxonomy.
if ($entity->getEntityTypeId() == 'taxonomy_term'
&& $entity->$brand_color_field
&& !$entity->$brand_color_field->isEmpty()
) {
return $entity->$brand_color_field->value;
}
else {
return FALSE;
}
}
/**
* Implements hook_preprocess_node().
*/
function sfseed_preprocess_node(array &$variables) {
$node = $variables['node'];
// Set the category brand class color if it is available.
$variables['category_brand'] = FALSE;
if (theme_get_setting('use_category_colors')) {
$variables['category_brand'] = sfseed_get_category_brand($node);
}
}
/**
* Implements hook_preprocess_page_title().
*/
function sfseed_preprocess_page_title(array &$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
$term = \Drupal::routeMatch()->getParameter('taxonomy_term');
// Check if the node is just a node ID. If it is then the page is a node revision
// TODO: remove once https://www.drupal.org/node/2730631 is finished.
if ($node && !is_object($node)) {
$node = FALSE;
}
// Check first to make sure that this is an entity and not just a term id
if (is_string($term) || is_int($term)) {
$term = Term::load($term);
}
// Set the category class color if it is available
if (theme_get_setting('use_page_title_category_color')) {
if ($node) {
$variables['category_brand'] = sfseed_get_category_brand($node);
}
elseif ($term) {
$variables['category_brand'] = sfseed_get_category_brand($term);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment