Jump to content

ProcessPageEditImageSelect - hook and change default $page


mr-fan
 Share

Recommended Posts

hi there i've shouted a question to setup the default path in the pagetree of the imageselect in the RTE (eqal tiny or CKE)

user should only see a kinda image-root page where images and categories are pages - not the whole pagetree

so i've learned it have nothing to to with CKE - so it's a Admin->Page-Insert Image with the

ProcessPageEditImageSelect

working here.

So long ok

deeper i understand that in the getImages() function the second part is build to show the pagetree for choosing a other page with images...

so how can i get here only a pagetree with parent="image-root" ?

is this possible without deeper OOP skills?

simply hook and set the parent?

hints? options? expertise?

(I've a google search but didn't find any solution)

regards mr-fan

Link to comment
Share on other sites

So solutiion comes from adrian!

her the code: https://processwire.com/talk/topic/3023-module-ckeditor/page-10#entry71664

and here a small module (Parent ID could changed in the modulefile)

https://processwire.com/talk/topic/3023-module-ckeditor/page-10#entry71672

next step i will try and import from other modules is to get the setting of the ID in the backend....;)

PW is great and the people that drives this bus are greater!

Edited by Pete
Re-linked first link to adrian's solution as I couldn't find it
  • Like 1
Link to comment
Share on other sites

Next short question about changing the rootID of the ProcessPageList in a PageEditProcess:

in addition to adrians gist module -> https://gist.github.com/adrianbj/437e3945e9d774f5a67e

i'ld like to add a second select to the module like this:

<?php
class ChangeImageSelectParent extends WireData implements Module, ConfigurableModule {

    /**
     * Data as used by the get/set functions
     *
     */
    protected $data = array();

   /**
     * Default configuration for module
     *
     */
    static public function getDefaultData() {
            return array(
                "rootParentImage" => "",
                "rootParentFile"  => ""
            );
    }

    public static function getModuleInfo() {
        return array(
            'title' => 'Change Image and File Select Parent',
            'version' => 1,
            'singular' => true,
            'autoload' => true
            );
    }

    /**
     * Populate the default config data
     *
     */
    public function __construct() {
       foreach(self::getDefaultData() as $key => $value) {
               $this->$key = $value;
       }
    }

    public function init() {
        $this->addHookAfter('ProcessPageEditImageSelect::execute', $this, 'changeParentImage');
        $this->addHookAfter('ProcessPageEditLink::execute', $this, 'changeParentFile');
     }

    public function changeParentImage(HookEvent $event) {
        $event->replace = true;
        $event->return = str_replace("rootPageID: 0", "rootPageID: ". $this->data['rootParentImage'], $event->return);
    }

    public function changeParentFile(HookEvent $event) {
        $event->replace = true;
        $event->return = str_replace("pageID: 0", "rootPageID: ". $this->data['rootParentFile'], $event->return);
    }

    /**
     * Return an InputfieldsWrapper of Inputfields used to configure the class
     *
     * @param array $data Array of config values indexed by field name
     * @return InputfieldsWrapper
     *
     */
    public static function getModuleConfigInputfields(array $data) {

        $data = array_merge(self::getDefaultData(), $data);

        $wrapper = new InputFieldWrapper();

        $f = wire('modules')->get('InputfieldPageListSelect');
        $f->attr('name+id', 'rootParentImage');
        $f->label = __('Root Parent Image', __FILE__);
        $f->columnWidth = 50;
        $f->description = __('The root page for the Image Select dialog to start from.', __FILE__);
        $f->attr('title', __('Root Parent Image', __FILE__));
        if(isset($data['rootParentImage'])) $f->value = $data['rootParentImage'];
        $wrapper->add($f);

        $f = wire('modules')->get('InputfieldPageListSelect');
        $f->attr('name+id', 'rootParentFile');
        $f->label = __('Root Parent File', __FILE__);
        $f->columnWidth = 50;
        $f->description = __('The root page for the Insert File Select dialog to start from.', __FILE__);
        $f->attr('title', __('Root Parent File', __FILE__));
        if(isset($data['rootParentFile'])) $f->value = $data['rootParentFile'];
        $wrapper->add($f);

        return $wrapper;
    }

}

The setting in the module works right - choose both rootparents.

Images work - files not....i've searched in the module and js files of the ProcessPageList and the ProcessPageEditLink

but still to less skill to stumble over the right piece of code.

Link to comment
Share on other sites

Sorry again - stupid idea! since i can't select other pages here...

i use somas trick with files as pages and would have a select of files with the rootPage as the documents-page (parentpage for all docs)

so this could not work as expected.

i'll would have to modify the insert link modal to get whant i want - and this is far away for my skills.....so i know my borders. ;)

So i'll leave this - for the images it works great!

Link to comment
Share on other sites

  • 1 month later...

I have a special use case where I need to change the rootParent to a page owned by the logged in user that has the template "media".

I think adrian's module with some modifications is exactly what I need, but I'm not quite sure how to modify it the right way.

Here is what I've got so far.

class ChangeImageSelectParent extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Change Image Select Parent',
            'version' => 1,
            'singular' => true,
            'autoload' => true
            );
    }

    public function init() {
        $this->addHookAfter('ProcessPageEditImageSelect::execute', $this, 'changeParent');
    }

    public function changeParent(HookEvent $event) {
        $userID = wire("user")->id;
        $rootPageID = wire("pages")->get("template=media, created_users_id=$userID")->id;
        $event->replace = true;
        $event->return = str_replace("rootPageID: 0", "rootPageID: ".$rootPageID, $event->return);
    }

}

Any help would be much appreciated.

Link to comment
Share on other sites

Thank you, adrian, for looking into this.

I think I need to get the user with $userID = $this->wire("user")->id; Am I right here?

But I can't really check if it is working because I get an error when clicking the PWimage button in the editor on my frontend form.

I opened an extra thread for that problem.

Sorry for cross posting here, but I thought I better check here for the validity of my module code.

Cheers

Gerhard

Link to comment
Share on other sites

OK, done testing in backend. With this code

    public function init() {
        $this->addHookAfter('ProcessPageEditImageSelect::execute', $this, 'changeParent');
    }

    public function changeParent(HookEvent $event) {
        $user = wire("user");
        echo "User:".$user->name." ID:".$user->id."<br>";
        //if ($user->role == "frontend") {
            $rootPage = wire("pages")->get("template=media, title={$user->name}");
            $rootPageID = $rootPage->id;
            echo "rootPage Title:".$rootPage->title." ID:".$rootPageID;
            $event->replace = true;
            $event->return = str_replace("rootPageID: 0", "rootPageID: ".$rootPageID, $event->return);
        //}
    }

I get

post-1920-0-97398300-1414425145_thumb.pn

and after click on "change" I get

post-1920-0-23177300-1414425188_thumb.pn

So it is working :)

But that is not what I need :-(

I'd like to choose images from the rootPage itself and not it's children. And I'd like to have the rootPage chosen by default instead of the page I'm on when I click on the insert image button.

Would this be possible to achieve with adding other hooks to the ProcessPageEditImageSelect.module?

Link to comment
Share on other sites

The page I want to choose images from is the one I get with

$rootPage = wire("pages")->get("template=media, title={$user->name}");

In the 1st screenshot that page title and ID are echoed right on top in the second line:

rootPage Title:gbr ID:11582

But then it says: "Images on Page: now (/advertisements/ad-now-1035)". This is the page where the CKeditor field is on.

Here is where I need my rootPage. So it should read:

"Images on Page: gbr"

and show me the images on that page. That page contains only an images field with images in it (and the title field).

Link to comment
Share on other sites

Ok, all makes sense now :)

Your needs are different from the person I put that together for - they wanted the root parent changes, not to actually drill down to the specific page.

You can get directly to the specific page with this:

    public function changeParent(HookEvent $event) {
        $pid = (int) $this->input->get->id;
        $event->replace = true;
        $event->return = str_replace($pid, $rootPageID, $event->return);
    }

BUT unfortunately this doesn't load the images available on the page because the check to see if there are images available or not happens here:

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Process/ProcessPageEditImageSelect/ProcessPageEditImageSelect.module#L129

which is before the execute method we are hooking into.

So I think you'd actually have to hook directly into ProcessPageEditImageSelect::getImages which is not currently hookable. But maybe you should add the ___ to it and play around and see if you can get it work. Sorry I don't have time right now, but I think it should be doable. If you get it to work, Ryan should be amenable to making it hookable in the core.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...