Orkun Posted June 22, 2016 Share Posted June 22, 2016 Is it possible to manipulate the Form of the ProcessPageAdd with hooks or something? Like adding some other fields (from the corresponding Template) besides the title field. The values which you type in while creating the Page should also be adapted on the ProcessPageEdit of the created Page. Why I need this? I have a newsletter template with different multilanguage fields (using 5 Languages). The user has the ability to create new newsletter pages in different languages. But now there is a little problem of understanding the workflow. Let's say I want to create a new newsletter Page called "20160621_This_is_a_example_Newsletter" which only has two language versions of content. When editing the page I see 5 Lanuage Tabs per field and I fill up the ones with content that I need, which is normal. But now every time the user edits a newsletter page he will also see a empty language tab when the default language is empty (can be empty because fields are not required and perhaps the user doesn't need a newsletter which uses the default language) which can be confusing. To solve the confusion of the user I want to implement a field (type of PageListSelectMultiple) inside the ProcessPageAdd where you can choose in which language the new newsletter should be / will be. After that It should only show the Language Tabs which the user has selected on the ProcessPageAdd respectively the ones he really needs to fill up. This makes the Workflow a lot easier for the user. So the last thing to mention is - have you any idea how to make this work? Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted June 22, 2016 Share Posted June 22, 2016 I am not sure it is even possible at all, at least looks like it is not so easy to do. But this seems to me the case you can and should manage by organizational means like docs and some education for the managers. Maybe add some kind of field for notes, so you can give page specific instructions. Link to comment Share on other sites More sharing options...
Orkun Posted June 24, 2016 Author Share Posted June 24, 2016 This worked halfway for me: // First define your Hooks public function init() { $this->addHookAfter('ProcessPageAdd::buildForm', $this, 'addFields'); $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'restrictLanguages'); } // Function for adding Fields to ProcessPageAdd public function addFields(HookEvent $event){ $form = $event->return; if($this->input->parent_id){ if($this->pages->get($this->input->parent_id)->template->id == 68){ $fieldsToAdd = array('is_internal_newsletter', 'languages_active', 'title'); foreach ($fieldsToAdd as $field) { $fieldForPage = $this->fields->get($field)->getInputfield($this->page); if($field=="title"){ $fieldForPage->label = "Filename"; } $form->prepend($field); } } } } // Function for disabling(unpublishing) Language Pages which are not checked in the field "languages_active". public function restrictLanguages(HookEvent $event){ $page = $event->object->getPage(); if($page->languages_active->count){ foreach($this->languages as $language){ if(!$page->languages_active->has($language)){ $language->addStatus(Page::statusUnpublished); } } $this->languages->reloadLanguages(); } } It works correctly but still it has some inaccuracies. - restrictLanguage Function doesn't effect at creation. It starts working after the first save. - when adding new items to repeater and saving afterwords it changes to its normal state (all language tabs are shown). But saving for the second time after added the repeater item, it starts working again. Have you some suggestions to fix the inaccuracies? Greetings Nukro Link to comment Share on other sites More sharing options...
Orkun Posted June 28, 2016 Author Share Posted June 28, 2016 Changed it completely, now it implements a little bit css with a hook inside InputfieldForm::render which does the magic of hiding and showing fields depending on language checkboxes. Since it's only a visual thing I removed the older hook where the languages get unpublished depending on the checkboxes. It just creates more bugs when playing around with the languages itself because the languages are a widespread feature in ProcessWire which are affecting other classes. With the new hook I am preventing that in some way since I have more control over what I am doing. public function init() { $this->addHookAfter('InputfieldForm::render', $this, 'renderForm'); } public function renderForm(HookEvent $event) { $id = $event->object->attr("id"); if($id != "ProcessPageEdit") return; $pageID = (int) $this->input->get->id; if(!$pageID) return; $page = $this->pages->get($pageID); $_out = ""; $index = ""; if($page->id){ foreach($this->languages as $key => $language) { if(!$page->languages_active->has($language)){ $_out .= "div[data-language='{$language->id}']{ display: none!important; }\n"; if($language->isDefault()){ $_out .= ".langTabs > ul li:first-child{ display: none!important; }\n"; } else { $_out .= "li[aria-controls$='__{$language->id}']{ display: none!important; }\n"; } } else { if(!$index) $index = $key; } } if($index) { $tabJSConfig = $this->wire('config')->js('LanguageTabs'); $tabJSConfig['activeTab'] = $index; $this->wire('config')->js('LanguageTabs', $tabJSConfig); } } if($_out) $_out = "<style>$_out</style>"; $event->return .= $_out; } Example: The above selection creates this inline CSS: <style> div[data-language='1029']{ display: none!important; } li[aria-controls$='__1029']{ display: none!important; } div[data-language='4286']{ display: none!important; } li[aria-controls$='__4286']{ display: none!important; } div[data-language='5842']{ display: none!important; } li[aria-controls$='__5842']{ display: none!important; } </style> 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now