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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Idea] AbstractObject ->getClosestParentOfType() #5492

Open
romainbessugesmeusy opened this issue Dec 19, 2019 · 1 comment
Open

[Idea] AbstractObject ->getClosestParentOfType() #5492

romainbessugesmeusy opened this issue Dec 19, 2019 · 1 comment

Comments

@romainbessugesmeusy
Copy link

@romainbessugesmeusy romainbessugesmeusy commented Dec 19, 2019

Hey there, happy holidays to everybody 馃巹

I think anyone who has been working with jQuery know the value of the method closest(selector) which allow the developer to go the nearest parent element that matches the selector. I hereby propose to add a somewhat similar method to the AbstractObject class, in the form of a new method that would allow developers to gain access to the closest parent of a certain type or class.

Here's a concrete example:
Capture d鈥櫭ヽran 2019-12-19 脿 22 42 44

I have objects that are instances of PrintingTechniques (i.e. Screen Printing) and I use them as folders to store PrintingOptions (i.e. Press Type). I could create a manyToOne property called technique in the PrintingOptions class, restrict it to Objects of type PrintingTechniques and use the magic method $printingOption->getTechnique(). But this means that I have to ask the users to make sure that this field is correctly filled with respect to its parent Technique in the tree.

I could also listen to events in the tree and programmatically update this field, but it feels like using a bazooka to kill a mosquito.

Instead, I would rather use a method like :

$printingOption->getClosestParentOfType(PrintingTechnique::class);

I noticed the Concrete class already implements a similar logic in the method getNextParentForInheritance. The proposed method getClosestParentOfType would simply call the getParent() until it the instanceof condition matches.

By the way, I feel it would also resolve the issue raised here @dpfaffenbauer : #1944

What do you think? Do you think this could make its way through the core?

Thank you

@romainbessugesmeusy

This comment has been minimized.

Copy link
Author

@romainbessugesmeusy romainbessugesmeusy commented Dec 25, 2019

Here's what it could look like in the Concrete Class :


    /**
     * @return AbstractObject|null
     */
    public function getNextParentForInheritance()
    {
        return $this->getClosestParentOfClass($this->getClassId());
    }

    /**
     * @param string $classId the Pimcore class id
     * @return AbstractObject|null
     */
    public function getClosestParentOfClass(string $classId)
    {
        if ($this->getParent() instanceof AbstractObject) {
            $parent = $this->getParent();
            while ($parent && $parent->getType() === self::OBJECT_TYPE_FOLDER) {
                $parent = $parent->getParent();
            }

            if ($parent && in_array($parent->getType(), [self::OBJECT_TYPE_OBJECT, self::OBJECT_TYPE_VARIANT], true)) {
                if ($parent->getClassId() === $classId) {
                    return $parent;
                }
            }
        }

        return null;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can鈥檛 perform that action at this time.