Jump to content

Renaming the "content" tab when editing pages of a given template


netcarver
 Share

Recommended Posts

Can anyone point me in the right direction to rename the "content" tab of pages in the admin page editor? I wan't to rename it when editing pages of one of my templates. It wouldn't surprise me if I had to hook a render method somewhere.

I've googled for this but not found anything particularly useful.

Thanks in advance! 

Link to comment
Share on other sites

could you try this:

$this->addHookAfter("ProcessPageEdit::buildForm", $this, "tryThis");

public function tryThis(HookEvent $event) {
    $form = $event->return;
    $content = $form->find("id=ProcessPageEditContent")->first();
    $content->label = 'steve';
}

sitting on the livin' room giving the baby milk, so no time to test what I wrote. :-)

  • Like 4
Link to comment
Share on other sites

@mr-fan

Thanks for the reply. I thought about that already but it does it for all labels. I only want to do it for the labels of one particular template's children. I'm actually getting a little closer to this now as Martijn's answer has pointed me in the right direction; which seems to be to hook buildFormContent().

Link to comment
Share on other sites

Furthermore, does anyone know how I can actually removing the visual "tab top" altogether (not the contents of the tab, just the rectangle at the top). This is because I have a template set to have "no settings" ($template->noSettings = 1), so there is *only* the content tab being shown... and it is redundant and wastes space when I show the edit fields in a modal.

Link to comment
Share on other sites

From the source code it looks like the title of the first tab is set by adding the title attribute to the form in buildFormContent. I can successfully overwrite that attribute with this code...

wire()->addHookAfter("ProcessPageEdit::buildFormContent", $this, "tryThis");

public function tryThis(HookEvent $event) {
    $form = $event->return;
    $new_title = $this->_("Personal Info");
    $title = $form->attr('title');
    $this->message("Updating form title [$title -> $new_title]");
    $form->attr('title', $new_title);
}

And I've verified (by single stepping the code) that the attribute gets overwritten and returned and even makes it through several stages of hook return. Despite this, as Martijn mentioned, it seems to be protected as it still gets rendered as "Content" even with the change. I have no idea why at the moment.

Update 1: Ok, it is getting saved in an instance variable ($this->tabs[$id] = $title) and it is that variable that's being used to render the tab via WireTabs. Will need to look at hooking execute() and doing a replace on the rendered output. Not a pretty way to do it - but at least I know it should work.

Update 2: Just to confirm, I have succeeded in doing this using an after hook on ProcessPageEdit::execute - but it's not a particularly elegant solution.

Edited by netcarver
  • Like 4
Link to comment
Share on other sites

@jordanlev

Sure, but be warned, my code currently does it across all pages you edit as I've not added anything to restrict it to just the template I'm after. In fact, I'm now worried that I won't be able to do it on a per-template basis this way. But the day is late and I'll worry about that tomorrow. In the meantime, here it is...

public function init()
{
    wire()->addHookAfter("ProcessPageEdit::execute", $this, "tryThis");
}

public function tryThis(HookEvent $event) {
    $render = $event->return;
    $event->return = str_replace("Content</a>", "New Title</a>", $render);
}

That's part of a little auto-loaded module.

Oh, yeah, it also currently updates any links that end with the word "Content" but that's fixable with a more complex match.

  • Like 2
Link to comment
Share on other sites

@Martijn,

I've unmarked this as solved as it's not quite the whole story. If I put a breakpoint in my hook routine I can examine the $event object and I can see all the information I need to be able to make a rename decision as the $event object's page's template has a title that I want to compare against - but it doesn't seem possible for me to access that information via the API using $event->object->page->template->title which always gives a blank, despite me being able to inspect the data in the debugger. I suspect that some of these fields are protected or private members of the $event->object.

If I do find a full solution I'll post it.

  • Like 2
Link to comment
Share on other sites

Ok, as promised, here's my solution.

public function init()
{
    wire()->addHookAfter("ProcessPageEdit::execute", $this, "tryThis");
}

public function tryThis(HookEvent $event)
{
    $render = $event->return;
    $template_name = "xyz"; // Change this to match the exact template name of pages you want to apply tab-renaming to.
    if (false !== strpos($render, "template_{$template_name} ")) {
        $render = str_replace("Content</a>", "New Title</a>", $render);
        $event->return = $render;
    }
}

I'm not totally satisfied with it as it makes no allowance for possible changes to the default string or the internationalisation of it - but it is still relatively easy in PW. I've raised an issue about this over on the home repo as I think PW should be able to solve this more elegantly.

post-465-0-39797200-1413571544_thumb.png

It will do for this current site as I only expect to use the English default string for the content tab.

  • Like 4
Link to comment
Share on other sites

Thanks for that guys.

This solution is quite brittle and may break with future releases of PW as it relies on the current way that PW marks up the submission form with a class that contains the template name. If PW ever stops adding that markup class, this will break.

Also take note of that space after the end of the template name in the search string - which is needed if you have templates with matching substrings: like "patient" and "patients" where both would match if you tried to apply it only to "patient" and don't have the space at the end.

I'd much rather be able to access the template name directly somehow from the hook method (preferably as the form is built rather than post-rendered) but I just couldn't find the right way to access it.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...