News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

retrieve theme data from database for new button

Started by jms1989, April 22, 2015, 12:50:58 AM

Previous topic - Next topic

jms1989

I am wanting to use code in my theme to add a home page url and title then print that to a button next to the forum home button. how would I do this? I can't seem to figure out how to pull info from the database.

Also, I am forced to mod the Subs.php file to include this button, is there a theme file where this would be better suited?

margarett

Are you using 2.1?

Buttons are not stored in the database. You need to either edit Subs.php or create a hook and edit the menu array in it.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

jms1989

Ok, I would prefer to not have to modify the core files to add a button for my theme. I have created a couple extra variables for the template_settings function to then store the button name and url in the forum database, theme table. I also edited the language file for the settings text. I want to retrieve the data from the database for this button. I'm not very good at php coding but do good with html and css.

How can I use hooks to do this within the theme files?

margarett

The "problem" is that the buttons are not theme-dependent, although you can change the buttons array at any time before display.
I'm not exactly sure out of my head how you can do this and I'm on the phone now so I need to check later. Nudge me if I forget :P

Again, are you using 2.1?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Kindred

basically, the 2.0 and 2.1 series use the array in subs.php to build the menu....

the hooks just use php code to insert the SAME THING into the array...

in other words - nothing in the menu array uses the database.

What you are trying to do COULD be coded, but would involve you re-writing the entire menu array to pull form the database...

that has been done as a mod by one or two authors... but even those cases do not allow extra variables as you have attempted to do.

Since you won't answer what version - and this is clearly not a support question and more of a coding question, I am moving the thread to the coding board.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

jms1989

Sorry, I didn't see the question before. Yes, I am developing my theme for 2.1. I like the responsiveness of Curve2 so I decided to base my theme off that. I am aware that 2.1 is still in beta but seems to be quite functional despite that. I do have other issues (mail either won't send or lands in spam) but that's for another topic.

I'm not familiar with the use of hooks and I know I can simply hard code the url values into the array in Subs.php but I don't want to edit core files, I want it all within the theme files. I'm not sure where I should add link code to index.template.php or if there is another file responsible for it.

This is what I added to Settings.template.php to create custom values in the database for said button.

array(
            'id' => 'homepage_url',
            'label' => $txt['homepage_url'],
            'description' => $txt['homepage_desc'],
            'type' => 'text',
        ),
        array(
            'id' => 'homepage_name',
            'label' => $txt['homepage_title'],
            'type' => 'text',
        ),


This doesn't seem possible for what I want to do aparently. :(

jms1989

Ah, I've figured it out. I added this bit to index.template.php...


                echo '<li id="button_homepage">
                            <a href="', $settings['homepage_url'], '"><span class="generic_icons homepage"></span>', $settings['homepage_name'], '</a>';


Just below <ul class="dropmenu" id="menu_nav">'; in the template_menu function then appended $settings to the global variables within that function. This resulted in a button appearing before the array and using variables I set from the theme settings page.

Another idea is to include an option to hide my button, should I choose, for reasons. To have a check box on the theme settings page to enable/disable the button.

I am going to change the icon for the forum home button to something better suited perhaps. Then some css foo to change it back if the homepage button is disabled by the aforementioned checkbox. Attached is how it appears at the moment:

Kindred

But, see, that is the very thing that we UNDID in 2.0 and forward...   Menus do not belong in the template files... They belong in the source files, currently... And, eventually, in 3.0, in the database...  But never in the templates
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

live627

Add this to $buttons

'homepage' => array(
'title' => !empty($settings['homepage_name'] ? $settings['homepage_name'] : $txt['home'],
'href' => !empty($settings['homepage_url'] ? $settings['homepage_url'] : $scripturl,
'show' => true,
),


This will work across all themes.

jms1989

It's just one custom button for my theme. :o  just to avoid modding core files.

Kindred

WHY are you trying to "avoid modifying core files"?

That's what a custom installation (which describes each individual forum) is for!
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

jms1989

I just think it's silly to mod core files, what if there was an update and it changed a modded file without me knowing? I just like the idea of restricting changes to themes and plugins, much like how WordPress does it. I like smf but changing core files makes it seem less modular. Putting all the changes in the theme itself gives me piece of mind that my changes will remain across updates.

margarett

It's one way of seeing it and it is entirely possible by using hooks or theme edits. If you use only 1 theme you get no advantage of the global menu defined in Subs.php so it's all good.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Kindred

well, the menu in subs.php is also used for things like SSI as well.... :)

And updating the core files is generally acceptable jms, because of the way that SMF releases patches. :D   
The version to version upgrades change files in the same way as a mod -- which means that your don't lose any custom coding at all. :)

(note: I am not trying to convince you of anything at this point - we're just debating the process, repercussions and application. :) )
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

jms1989

I guess we'll see what happens when beta 2 is released or some version following 2.1 beta. I don't think I've ever gone thru the update process. I have a copy of 2.0.9 that has been live for 2 years or so now. My reasoning for using 2.1 is for the responsive curve2 theme. Very easy to customize btw. :)

I don't intend to use multiple themes but if I do, I'll either copy my button code or break down and use subs.pho. :p

Kindred

Oh... you are using the beta version?  (hmmm, must have forgotten that)

In that case, you are going to have to do a complete overwrite, every time... yes...    and, probably you will have to replace your theme as well, since the templates between beta1 and beta2 have already changed and will likely change more between beta 2, RC and gold


In reality, with 2.1 -- your best option is actually to build your own separate little php file and include it into the menu with hooks...  THAT will survive past all upgrades
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Illori

Quote from: Kindred on April 24, 2015, 08:15:07 PM
In reality, with 2.1 -- your best option is actually to build your own separate little php file and include it into the menu with hooks...  THAT will survive past all upgrades

actually hooks will no longer survive, they need to reinstalled after upgrade.php is installed. the file that was created will still exist but the hook will be uninstalled/removed.

jms1989

So what have I learned from this? By using a development program what must be overwritten upon upgrade, all core mods will be lost. Therefore, to safeguard my mods, I shall directly inject them into a child theme of the curve2 theme. Most of my edits reside in 3 or 4 theme files anyhow, between restructuring index.template.php and changing bookoos of code in style.css, I have just a little button for a link to home.

Now, if only I can make it hide itself if the respected row in the db tables is missing. I did throw it into a if statement with two !empty bits to check for empty data but fails if the bit is missing altogether. I'm not sure how to describe it other than, when you cleanly install the theme, the button appears but the moment you enter some data on the theme settings page then clear it, the button disappears. I've tried adding defaults but I'm having a little trouble finding a guide that describes the variables in the arrays for Settings.template.php.

Advertisement: