Little javascript snippet to save as a bookmark to make a quick "edit this page" feature


#1

javascript:void(window.open("/panel/pages" + window.location.pathname + "/edit", "_blank"));

If you save this as a bookmark, you can quickly get to a page to edit it.

One of our content editors came up with it.


#2

Nice but there is a downside. That will edit the current page. Often pages are made up in part of content from other pages.

I do it like this… make a snippet called site-edititem.php with this in it:

<!-- edit link -->
<?php if ($user = $site->user() and $user->hasPanelAccess()): ?>
<a href="<?= $site->url() ?>/panel/pages/<?= $hook->uri() ?>/edit" target="_blank" title="Edit content" class="admineditlink">
  <span class="fa fa-edit editpage"></span>
</a>
<?php endif; ?>

Then use the snippet like this:

For a direct page:

<?php snippet('site-edititem', ['hook' => $page]) ?>

In a loop:

<?php foreach($page->children()->visible() as $article): ?>
  <h3><?= $article->title()->html() ?> <?php snippet('site-edititem', ['hook' => $article]) ?></h3>
<?php endforeach ?>

That way you can jump to the actual page, rather then just the page its being displayed on. Great for pages that list out blog posts, for example.


#3

Yeah, I have been meaning to do something similar for a long time. Your post inspired me to actually do it. :slight_smile:

For anyone else, a style of “z-index; 99; position: absolute” does a good job of making the edit button not interfere with the page layout.


#4

Keep in mind that a call to $site->user() creates a session cookie. For all of you out there who don’t want to have any cookies on your site…


#5

But that can put it somewhere weird if the parent element somehow hasn’t got relative positioning. I styled mine as you would a Superscript tag so it was just up in the air on the end of a page title.


#7

Does it do it even if your not logged in?


#8

Yes, it makes no difference if you are logged in or not.

I just wanted to add that because we had several people here wondering where the cookies came from and had a hard time to figure it out.


#9

Sure :slight_smile: I was hoping it would only do it for actual panel users, and not for all site visitors, which would be fine if your trying to go cookie free.


#10

BTW. The same strategy also works for adding new pages to a blog etc. via a link to panel/pages/parent-page/add


#11

One more thing, @jimbobrjames:

While it is handy to put the condition into the snippet, I think such a pattern should be avoided. Better to put the condition into the template and only load the snippet if it returns true. Otherwise, you load something only to find out that it is not needed.


#12

Hrmmm. I can see the logic in that, however it adds more to the template. How about checking it in the controller? Is there a clean way to do that?


#13

You can wrap it into a function:

function editLink($page) {

  if($user = site()->user() and $user->hasPanelAccess()) {
    return new Brick('a', '<span class="fa fa-edit editpage"></span>', [
      'href' => url().'/panel/pages/'.$page->uri().'/edit',
      'class' => 'editlink',
      'target' => '_blank'
    ]);
  }
  return null;
}
<?= editLink($page) ?>

With you little snippet, it’s probably not important, but in general, I would avoid this pattern of putting the condition for the complete snippet into the snippet.