Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:36:57+00:00 2026-06-13T08:36:57+00:00

I have set a field called Colour in Page.php and for any child I

  • 0

I have set a field called Colour in Page.php and for any child I would like to grab the parent colour or loop through till it finds a parent that does have the colour field set.

I have a function below which seems to work in 2.4 but I cannot get to work in SS3 which I call inside a loop in templates as $Inherited(Colour).

Your help is appreciated

public function Inherited($objName) {
    $page = $this->owner->Data();
    do {

        if ($obj = $page->obj($objName)) {

            if ($obj instanceof ComponentSet) {
                if ($obj->Count()) {
                    return $obj;
                }
            } elseif ($obj instanceof DataObject) {

                if ($obj->exists()) {
                    return $obj;
                }
            } elseif ($obj->exists()) {
                return $obj;
            }
        }
    } while ($page->ParentID != 0 && $page = $page->Parent());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:36:58+00:00Added an answer on June 13, 2026 at 8:36 am

    Assuming your Colour field is a database field and not a relationship to another data object, add the following method to your Page class.

    public function getColour() {
    
        // Try returning banners for this page
        $colour = $this->getField('Colour');
        if ( $colour ) {
            return $colour;
        }
    
        // No colour for this page? Loop through the parents.
        $parent = $this->Parent();
        if ( $parent->ID ) {
            return $parent->getColour();
        }
    
        // Still need a fallback position (handled by template)
        return null;
    }
    

    If colour is a related data object you could do much the same thing but use the getComponent or getComponents method in place of getField in the code above. This should work on both Silverstripe version 2.4.x and 3.0.x.

    This kind of operation, although useful, should probably be done sparingly or be heavily cached as it’s recursive could conceivably happen on the majority of page loads, and change very infrequently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have excluded a field like this in order to set it's label. class
Situation: Have a SET field called country with values ('us','uk','fr','intl') When I go to
I have a mysql table field set as time type which stores data in
I have a SQL database that has a table with a field set to
In my hello world application, i have a button and a text field set
I have a table with ' id ' as the primary field and set
I currently have my code set to disable the submit button when the field
I have a field aliases of type java.util.Set in one Entity . This Set
I have an input field that is by default set to type=text so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.