惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Y
Y Combinator Blog
F
Full Disclosure
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
S
SegmentFault 最新的问题
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
GbyAI
GbyAI
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
L
LangChain Blog
T
Tenable Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog

OSU Open Source Lab

Data Center Migration Update and Fundraising Campaign | OSU Open Source Lab OSL Infrastructure Migration: A Move to Oregon's State Data Center | OSU Open Source Lab Featured: Strong support stabilizes funding for the Open Source Lab | OSU Open Source Lab We're Hiring: Join the OSU Open Source Lab as a Student Systems Engineer! | OSU Open Source Lab Forging Our Future: OSL's Path to Sustainability – A Call for Smart Solutions and Enduring Support | OSU Open Source Lab Future of OSL in Jeopardy | OSU Open Source Lab Now Providing Access to POWER10 for Open Source Projects | OSU Open Source Lab FTP Server Rebuild - March 2024 | OSU Open Source Lab On Leaving the Open Source Lab, Jonathan Frederick | OSU Open Source Lab Reflections on My Time at the Open Source Lab, Travis Whitehead | OSU Open Source Lab OSL Alumnus Matthew Johnson on working at Tesla | OSU Open Source Lab Hiring two DevOps student positions | OSU Open Source Lab TDS Telecom Support of OSU Open Source Lab Tops $5 Million | OSU Open Source Lab Goodbye Letter from Graduating Senior, Cody Holliday | OSU Open Source Lab Mohamed Eldebri on OSL's participation in the Corvallis Maker Fair | OSU Open Source Lab Cody Holliday on the Department of Energy Cyber Defense Competition 2018 | OSU Open Source Lab OSL's Cody Holliday Wins Regional DOE Cyber Defense Competition | OSU Open Source Lab OSL Alumnus Alex Plovi sells CoreOS to RedHat | OSU Open Source Lab Changing the World, One Line of Code at a Time | OSU Open Source Lab Jonathan Frederick on Packer Templates project at the OSL | OSU Open Source Lab Ganeti Production Rebuild - Dec 11-15 & 18-19, 2017 | OSU Open Source Lab Thank You for Supporting our Crowdfunding Campaign! | OSU Open Source Lab A Message from the Director | OSU Open Source Lab New Project: polr | OSU Open Source Lab Donate to Our Crowdfunding Campaign! | OSU Open Source Lab Cody Holliday on Why we should stop using C | OSU Open Source Lab Amanda Kelner on Graduating | OSU Open Source Lab Beaver BarCamp 17: New Horizons | OSU Open Source Lab New Project: libpng Now Mirrored on ftp.osuosl.org | OSU Open Source Lab Network Outage 2016-12-17 Post-mortem | OSU Open Source Lab OSL Summer 2016 Internship | OSU Open Source Lab The OSL at PyCon | OSU Open Source Lab Congratulations 2016 Graduates! | OSU Open Source Lab RTEMS | OSU Open Source Lab CASS and the AllSeen Alliance | OSU Open Source Lab DevOps DayCamp 2015 | OSU Open Source Lab OSL GSOC 2015 - Oregon's Catch | OSU Open Source Lab OSL GSOC 2015 - Protein Geometry Database | OSU Open Source Lab Mysql1-vip Outage Post-Mortem | OSU Open Source Lab Write the Docs '15 | OSU Open Source Lab Coming Soon: Beaver BarCamp 15 | OSU Open Source Lab SCALE x13 | OSU Open Source Lab A Successful DevOps DayCamp | OSU Open Source Lab DevOps DayCamp | OSU Open Source Lab Make Bash, Not War | OSU Open Source Lab A Recap of OSCON 2014 | OSU Open Source Lab Google Migration Post-mortem | OSU Open Source Lab Vim Trick FTW! | OSU Open Source Lab OpenStack on OpenPOWER | OSU Open Source Lab Protein Geometry - What the Heck is That? | OSU Open Source Lab Openstack's Horizon | OSU Open Source Lab DevOps Bootcamp Extends OSL Reach | OSU Open Source Lab 10 Projects Making a Difference | OSU Open Source Lab Connecting at Beaver BarCamp 12 | OSU Open Source Lab Top 10 Projects Impacting the Open Source Community | OSU Open Source Lab Top 10 Milestones | OSU Open Source Lab A Look Back at Beaver BarCamp 1 | OSU Open Source Lab OSL Announces 10 year Celebration; Attends OSCON | OSU Open Source Lab Open Source Bridge Promotes Community | OSU Open Source Lab OSL to Mentor Four in Google Summer of Code | OSU Open Source Lab OSL Staff Participate at DrupalCon 2013 | OSU Open Source Lab OSL's April 2013 Newsletter | OSU Open Source Lab Students & Community Members Learn Together at Beaver BarCamp 11 | OSU Open Source Lab OSL to Participate in Google Summer of Code 2013 | OSU Open Source Lab OSL Staff Mentors International Students Through Google Summer of Code | OSU Open Source Lab Students, Community and Professionals Collaborate at Beaver BarCamp 10 | OSU Open Source Lab From Support to Success | OSU Open Source Lab OSL in the Spotlight at Summer Conferences | OSU Open Source Lab OSL Advances With New Staff Members; Renewed Focus | OSU Open Source Lab Building a Bright Future With DevOps | OSU Open Source Lab Connect With the OSL at OSCON 2012 | OSU Open Source Lab Open Source, Open Future | OSU Open Source Lab Setting the Bar High | OSU Open Source Lab OSL 'Unconference' Approaches | OSU Open Source Lab Highlights From Beaver Barcamp 8 | OSU Open Source Lab Lots of Love from LinuxCon | OSU Open Source Lab All Aboard for Vancouver! | OSU Open Source Lab New Study to Investigate Use of Open Source Software for Securing the Energy Industry | OSU Open Source Lab A Little Love for the LUG | OSU Open Source Lab Highlights from OSCON 2011 | OSU Open Source Lab OSL Talks OSWALD and Ganeti at Open Source Bridge | OSU Open Source Lab OSUOSL Alum Eric Searcy Joins the Linux Foundation as IT Infrastructure Manager | OSU Open Source Lab Out and About with the OSL in June | OSU Open Source Lab Emily on Essentials: Robotics, Open Source and Life as New Student at the OSL | OSU Open Source Lab Special Student Attendee Packages for LinuxCon North America 2011 | OSU Open Source Lab OSL Newsletter - April 2011 | OSU Open Source Lab Visiting Prineville: OSL Tours Facebook's Open Compute Data Center | OSU Open Source Lab OSL Newsletter: A Look Back at 2010 | OSU Open Source Lab CrisisCommons is pleased to announce partnership with OSU's Open Source Lab | OSU Open Source Lab OSU Open Source Lab to assist young students in “Google Code-in” contest | OSU Open Source Lab TeachEngineering Project Receives Award from NSF's National Science Digital Library | OSU Open Source Lab Google Surpasses $1M in Donations to OSUOSL | OSU Open Source Lab OSCON 2009 | OSU Open Source Lab Catch us at Open Source Bridge | OSU Open Source Lab Hackathon at Open Source Bridge | OSU Open Source Lab Hardware Donation Expands OSUOSL Infrastructure | OSU Open Source Lab Flourish! Open Source Conference | OSU Open Source Lab OSUOSL Visit to Japan | OSU Open Source Lab Linux.com finds new home at OSUOSL | OSU Open Source Lab OSU Open Source Lab announces OSL Alliance Corporate Sponsorship Program | OSU Open Source Lab
Using Variables in a Custom Drupal Theme | OSU Open Source Lab
2014-10-01 · via OSU Open Source Lab

Using Variables in a Custom Drupal Theme

At the Open Source Lab we host many of our sites as a Drupal multisite. This means that we have several instances of Drupal using the same theme, and then we can populate each site with different content as needed (for instance, cass.oregonstate.edu and osuosl.org would be different websites with different messages, but with Drupal they can look and act the same. Pretty neat!). Since not all of our sites are used by the OSL (i.e. the Center for Applied Systems and Software), I recently needed to make our logo and organization name into variables so that the user could just upload an image and fill in a text box to customize the site theme to their organization. Luckily, Drupal makes my job pretty easy.

First, I created a file osuosl-setting.php. Since our theme is titled osuosl, there are some things I can hard-code under our company name and this seemed like one of them. The first step to magic variabilization is to alter the systems theme settings and the form that controls them. Drupal’s naming scheme is a little wonky (as you end up with things like hook_field_attach_prepare_translation_alter()), but in a roundabout way it makes sense. To digress a bit into this topic, let’s break down the osuosl_form_system_theme_settings_alter name. The first part of Drupal’s function names is the object that it is affecting. In this case, osuosl is the name of our theme, and the theme is the thing we are trying to add something to (you could also be trying to make changes to blocks, pages, hooks, and a variety of other things). Then, within the theme we are modifying the form_system. This includes any forms that are default in the Drupal theme, such as the settings form found under Appearance->Theme->Settings. Next, as seen in the previous example, we’re modifying the theme settings form. And finally, we are altering the form (as opposed to creating it, deleting it or setting variables within it). This is how we get to osuosl_form_system_theme_settings_alter. It’s not intuitive, but Drupal has excellent documentation to help you along. Now, on to some code!

<?php
function osuosl_form_system_theme_settings_alter(&$form, $form_state){
}
?>

Next, there are two variables that need to be added to the form. For each of these, we need to add them to the theme_settings array, and then make them into their own arrays to store all the necessary information about them.

<?php
    function osuosl_form_system_theme_settings_alter(&$form, $form_state){
        $form['theme_settings']['logo'] = array();
        $form['theme_settings']['site_name'] = array();
  }
?>

Now we’ll fill in some variables about the logo and name

<?php
function osuosl_form_system_theme_settings_alter(&$form, $form_state){
    $form['theme_settings']['logo'] = array(
          '#type' => 'managed_file',
          '#title' => t('logo'),
          '#required' => TRUE,
          '#default_value' => theme_get_setting('logo'),
          '#upload_validators' => array(
              'file_validate_extensions' => array('git png jpg jpeg')
              )
          );
    $form['theme_settings']['site_name'] = array(
          '#type' => 'textfield',
          '#title' => t('site_name'),
          '#default_value' => theme_get_setting('site_name')
          'required' => TRUE,
          );
}
?>

Now that osuosl-settings.php was done and my variables were ready to be put into the theme, I needed to add some defaults for them so that if the user didn’t upload a logo or org name, there would still be something there. I decided to use the OSL logo and organization name under the assumption that the theme will be used primarily by us. So in osuosl.info, I added the lines:

  settings[logo] = images/logo-full.png
  settings[site_name] = OSU Open Source Lab

Finally, I had to call my variables in the Drupal templates so that they would be rendered when the site was built!

<a href="/">
  <img src="<?php print theme_get_setting('logo'); ?>" alt="<?php print (theme_get_setting('site_name'));?>" />
</a>

Ta-da! Now, all the user has to do is go to Appearances->Theme->Settings to upload a new logo and fill in their organization name!