Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

quicksketch
Copy link

Builds on backdrop#3422 by doing the following:

  • Renames "user_from_node" relationship plugin to "author_from_node"
  • Moves logic from layout_get_context_from_relationships() to a private method Layout::getContextFromRelationships()
  • Builds in loading of context relationships directly into 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 the LayoutContext class.

Comment on lines +12 to +29
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;
Copy link

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?

Copy link
Author

@quicksketch quicksketch May 2, 2021

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().

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @quicksketch 🙏🏼

@quicksketch quicksketch force-pushed the new-relation branch 3 times, most recently from d4b492e to df1d11f Compare May 2, 2021 01:12
@quicksketch
Copy link
Author

I'm continuing final changes in a separate core PR at backdrop#3611. No need to merge this any more.

@quicksketch quicksketch closed this May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants