Skocz do zawartości
Zaloguj się, aby obserwować  
Invisionize.eu

[Invisionize.eu] Michael's Blog - Hook Walkthrough: Multiple files, member cache (part 1)

Polecane posty

For the next walkthrough I'm going to do, I'd like to try to make a hook that will let members choose whether they want to hide the sidebar on the board index. Yes, I know there is already an option to hide or show the sidebar, but I'm going to add a My Settings option where they can disable the sidebar altogether. It may not be too terribly practical of mod, but it will demonstrate a few different mod techniques.

I'd like to go into more detail with this walkthrough to really outline the choices I make at each step so that you can understand the entire flow of creating more complicated hooks like this. As such, I'll split this walkthrough up into multiple parts. It takes a lot longer to write these up than it does to actually do the work involved, so it may be a couple of days between new parts of this walkthrough.

First off, we need to plan how this is going to work. The ultimate goal of the hook is to make it so members get a new My Settings checkbox, and then when it is checked and the form is saved, the sidebar on the board index disappears completely, without having the option there to hide or show it, as if there were no available sidebar blocks to display. So what all do we need to do to make that work? Well, for starters, we need to hook into the My Settings page to get the checkbox to display. We then need to hook into the action of saving the My Settings page and get it to Store the value of that checkbox somewhere accessible later on. And then we need to read that value and use it to determine whether the sidebar should show. So for this walkthrough, we should have three separate hook files in our hook:

  • A template hook to display the checkbox
  • A library hook or action overloader to save the value of the checkbox on the form
  • A skin overloader to redefine the board index data
So let's start with getting the checkbox to show. The way we'll do this is by hooking into the My Settings page. We first have to decide where we'll put this checkbox, so the way we do that is by looking at both the actual My Settings page on the board as well as the skin template for it; we have to look in the template because we need to find a hook point where we can add our new checkbox. If we look at the actual My Settings page, we see it is actually made up of a bunch of tabs on the left for different sections. So for starters, we have to decide which of these tabs we want it on. We'll just stick with adding in on the first tab, Profile Settings, because none of the others make too much sense.

Looking at this page, we can see that here we have further categorization of the different settings. So we have to decide whether we want to add our new checkbox into one of these existing categories or make a whole new one. For this step, we really need to also look at the skin template as well because it will dictate where we can possibly add this; there might be a place that we think makes sense, but no hook point exists there. So we jump over to our Admin CP and go to the Look & Feel tab, and then to the IP.Board skin. I happen to know that the skin template we're looking for is the User Control Panel -> membersProfileForm skin template, so we pull that one up (sounds like the way to figure this out may make for a decent blog entry). So back on the public side, let's look at the available categories we have and see if any of them make sense for our new setting. On my test board, I have these as my categories:

  • Photo
  • Time Zone
  • Comments & Visitors
  • Friends
  • Profile Information
  • Contact Methods
  • Profile Information
You may have others, depending on hooks you have or your custom profile fields, but to me none of these make sense for a place for our new setting. So what I'll want to do is add a new category with just our new setting. So if we look at the markup of the page, we see that it is divided up into separate <fieldset> elements for each category, with an <h3> for the title, and then a <div> with a <ul> in it for the settings in that category. So what we'll need to do to add a new category and setting is to find where we can hook either before or after each of those fieldsets. Looking at the template, I can see that there's a hook point around the Profile Information <fieldset>, before we get to our custom profile fields. Adding a new section between these two makes sense, so that's where we'll add it.

So now we are ready to start building our first hook file, to show the new setting. On the Manage Hooks page in the Admin CP, create a new hook and start filling out the settings for it:

We'll skip the Requirements and Global Caches tabs for now and go right to the Files tab. From here, we'll add a new file and start defining its hook point. Note that since we'll have multiple files for this hook, we'll name each as appropriate for the work it is going to be doing:

Before we save, we need to create the file with our code. So fire up your favorite code editor and make a new file. Since this is a template hook, we start with the basic skeleton code like this:

<?php

class disableSidebarCheckbox
{
	protected $registry;
	
	public function __construct()
	{
		/* Make registry objects */
		$this->registry = ipsRegistry::instance();
	}
	
	public function getOutput()
	{
		/* Send output */
		return '';
	}
}
Note that the class name is what we defined in the Admin CP above. Now this code is obviously extremely basic and won't do anything yet, so we should go ahead and tweak it a bit before we save it. In our getOutput function, we need to return the HTML that needs to display at this point. Displaying new HTML should always be done with a skin template, so we'll want to return a call to a new skin template we'll be creating. It doesn't exist yet, but let's go ahead and add the code that will call it now:
	public function getOutput()
	{
		/* Send output */
		return $this->registry->output->getTemplate('ucp')->hookdisableSidebarCheckbox();
	}
OK, now we save the file using the file name we entered above in the Manage Hooks section and save this file in our test board's /hooks directory. Once the file exists, we can jump back over to the Manage Hooks page and save the hook we've started. Finally, for this little bit we've worked on so far, we need to create that skin template we just referenced in our code. In the Admin CP Look & Feel tab, go to the IP.Board skin and click the button to add a new template bit. The name will be hookdisableSidebarCheckbox, and then choose 'skin_ucp' from the Add to Group dropdown, and click Add.

As we discussed earlier, this page has a very distinct HTML structure. So that's what we need to define here. As we're simply developing this at this point, and it's not a huge project, I find it easier to jump right in and code my templates with the actual text that will display as opposed to creating language strings at this point. We will have to go back and do that later, though. Here's the code we can put in that template to display the new setting:

<fieldset class="ipsSettings_section">
	<h3 class="ipsSettings_sectiontitle">Customization</h3>
	<div>
		<ul>
			<li>
				<input class="input_check" type="checkbox" name="disable_sidebar" id="disable_sidebar" value="1" />  <label for="disable_sidebar">Disable the Board Index sidebar?</label>
			</li>
		</ul>
	</div>
</fieldset>
So note that we have defined the id of this checkbox as "disable_sidebar"; we'll need to refer back to that later when we process the results of the form. Also note that we're not currently doing anything about showing the 'checked' state of this checkbox. We'll need to work on processing the results of the form before we can do anything with that part.

At this point, with the hook created and active, and the skin template it needs to call created, we can visit our My Settings page to see the results of this:

This concludes the first part of this walkthrough. For the next part, we'll be creating a file for this hook that processes the action of saving this page in order to save the value of whether this box was checked or not. As I'm making this mod at the same time as I'm writing this, I'm currently assuming that I'll be able to do this as possibly a library hook, or maybe an action overloader. I won't actually know myself until I dig into the code.

Feel free to let me know if there were any questions about any of this or things you'd like to know about future parts of this walkthrough. :smile:

Attached thumbnail(s)

  • Attached Image
  • Attached Image
  • Attached Image

Zobacz Artykuł w pełnej wersji

Udostępnij ten post


Link to postu
Udostępnij na innych stronach
Gość
Temat jest zablokowany i nie można w nim pisać.
Zaloguj się, aby obserwować  

  • Kto przegląda   0 użytkowników

    Brak zalogowanych użytkowników przeglądających tę stronę.

×

Ważne informacje

Kontynuując przeglądanie strony, wyrażasz zgodę na używanie przez nas plików cookies.