Defining a global edit mode variable

Permalink
In my last project, I had to build several custom blocks and in a lot of them the view was determined by whether the current page was in edit mode or not.

Typically, once I have the current page object ($c) in a block's view I simply do:
if($c->isEditMode()){
// foo
}else{
// bar
}


I took some time out to trace out what is actually occurring when you call this method and realizing it's not just returning some preset boolean value, I've been thinking of ways to check it once during the initial loading of the page and assign 1 or 0 to a global variable I can access from anywhere.

Has anyone done something like this before (without simply using $GLOBALS)?

beebs93
 
jordanlev replied on at Permalink Reply
jordanlev
Why do you want to do this?
I can't see any way to do this other than setting a global variable, unless you wanted to modify the core -- find the code in the base controller library that sets "$c" for every page type template, and have it also set your variable for this.

But really, doing that means someone else who sees your code is going to be confused. Even if it's clunky, the $c->isEditMode() is kind of a standard C5 thing that anyone familiar with C5 will recognize and understand.
beebs93 replied on at Permalink Reply
beebs93
I just finished a large project and while most pages don't have more than 2-3 custom blocks whose view each calls $c->isEditMode(), it seems a bit weird to call the same method over and over again when it should be defined once at the initial page render as a constant or in some time of application layer that can accessed easily.

While I understand this may be situational and the average C5 dev doesn't call this a lot, I've been using it a far amount in custom blocks, templates and/or elements that I feel I'm being inefficient with system resouces given that the method itself is not returning some preset boolean value, but rather, running a number of queries each time to fetch a simple yes/no.

Now, I concede I could missing a step somewhere as it's been over a month since I last looked at what's involved in running isEditMode(), but if I remember correctly it's not like, say, getting a page's custom attribute where once it's requested, C5 sets all the page's attributes to an object property so any future requests won't have to query it all over again.
jordanlev replied on at Permalink Reply
jordanlev
I see. Well, as they say "premature optimization is the root of all evil" -- you shouldn't worry about this unless you've done some kind of testing to see if this is actually a significant bottleneck (especially compared to other things that will definitely have a much huger impact on performance such as opcode caching, compressing/combining assets, gzip compression, and Apache KeepAlive/TimeOuts stuff).

If so, then I still think the only way to do it is with a global variable, and it will be tricky to figure out where exactly to declare that variable, because it needs to be done after the core system has set up the view.
beebs93 replied on at Permalink Reply
beebs93
Heh, "if it ain't broke, don't fix it". Guess I got ansy.

Thanks for the insight, though :)
beebs93 replied on at Permalink Best Answer Reply
beebs93
Just a quick update: I was digging through the main view model and noticed the $editMode variable is set within the scope of page templates with a boolean value denoting the current instance's edit mode status.

It still can't be retrieved within a block's view (without using globals) and it's not set as a view object property so View::getInstance()->editMode doesn't work (would be nice, though).