-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #2134: Converting context relationship functions into methods. #6
Conversation
const USAGE_TYPE_CUSTOM = 1; | ||
|
||
/** | ||
* Declares a LayoutContext to be provided based on a menu item or path. | ||
*/ | ||
const USAGE_TYPE_MENU = 2; | ||
|
||
/** | ||
* Declares a LayoutContext to be a system-wide context | ||
* | ||
* Examples include the "current_user" context and "overrides_path" context. | ||
*/ | ||
const USAGE_TYPE_SYSTEM = 4; | ||
|
||
/** | ||
* Declares a LayoutContext is provided by a relationship to another context. | ||
*/ | ||
const USAGE_TYPE_RELATIONSHIP = 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of curiosity, can someone explain to me why we are using what seem to me exponentially increasing numbers for these constants? What is the trick we're using them for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are for bitwise math operations, where binary numbers are used to indicate one "flag". These numbers correlate to the following in binary:
1 = 0001
2 = 0010
4 = 0100
8 = 1000
Each number results in a binary number with a single 1 digit and the rest zeros.
Then you can use multiple flags to request multiple types of layout usages by using bitwise math operators, like this:
USAGE_TYPE_CUSTOM | USAGE_TYPE_MENU
Which turns into 0011
(both 1 and 2). That means you can ask for multiple flags in a single argument instead of passing in several arguments, or having a bunch of IF statements that work on strings. This same pattern is used in the menu system in Backdrop, and is common in PHP functions, for example preg_replace_all()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @quicksketch 🙏🏼
d4b492e
to
df1d11f
Compare
I'm continuing final changes in a separate core PR at backdrop#3611. No need to merge this any more. |
Builds on backdrop#3422 by doing the following:
layout_get_context_from_relationships()
to a private methodLayout::getContextFromRelationships()
Layout::getContexts()
so that relationships do not need to be loaded separately. They can just be requested to be loaded at the same time as all other relationships.Making this last change required that we be able to differentiate between contexts that are added through relationships versus other mechanisms. This has been a problem with contexts from the beginning: You can't really tell where the context came from other than by a loose naming convention on the array keys (i.e. a integer comes from a menu path, "current_user" is always specially keyed, and now prefixing with "relationship_"). The way the context is added is now explicitly set in
LayoutContext::usageType
, a public property on theLayoutContext
class.