Jump to content

hookAfter Page::render causing all pages' fields to clear on unpublish ?!¿


hellomoto
 Share

Recommended Posts

For some reason when I unpublish an already-published page, even of a template other than the specified 'importer', all its fields are cleared on page render. How do I understand and remedy this? Peace & thanks


  public function init() {
	$this->addHookAfter('Page::render', $this, 'renderEditor');
  }
  protected function renderEditor(HookEvent $event) {
   $here = $event->object;
   $page = $this->pages->get($this->input->get->id);
   //$this->log->message($page->template.$here->process); return;
   if ($here->process != 'ProcessPageEdit' && $page->template != 'importer') return;

   $css = $this->config->urls->ProcessImportAll . 'ass/ProcessPageEdit_importer.css';
   $event->return = str_replace("</head>", "<link rel='stylesheet' type='text/css' href='{$css}'/>\n</head>", $event->return);

   $field = wire('fields')->get('name=pi_template');
   if (count($field->templateTypes)>0) {
     $templates = $field->templateTypes;
   } else {
     $templates = [];
     foreach (wire('templates') as $tpl) $templates[] = $tpl->id;
   }
   sort($templates); //$tplTypes = json_encode($templates);
   $templatesFields = $this->getTemplatesFields($templates);
   $templatesFields = json_encode($templatesFields);

   $js = <<<EOT
   <script>
   $(document).ready(function(){
     if (!document.getElementById('Inputfield_status_2048').checked) {
       $('#ProcessPageEditContent *').prop('disabled',true);
     } else {
       var all = $templatesFields;
       var sel = '#Inputfield_pi_template option[selected="selected"]';
       var tpl = getTpl();
       var arr = all[tpl];
       var def = '<option value=""></option>';

       $('#Inputfield_pi_match').prepend(def);

       filterFields(tpl);

       $('#wrap_Inputfield_pi_template .asmListItem .asmListItemRemove').click(clearFieldOptions);

       $('#Inputfield_pi_template').change(function() {
           var on = $(this).find('option[selected="selected"]')[0];
           if (on !== undefined) {
             tpl = on.value; //console.log('clicked ' + tpl);
             filterFields(tpl);
           }
           else { tpl = null; clearFieldOptions(); }
       });

       function filterFields(t) {
         if (t != null) {
           $('#Inputfield_pi_match').prop('disabled',false);
           arr = all[t]; //console.log(arr);
           $('#Inputfield_pi_match option').each(templateFieldOptions);
           $('#wrap_Inputfield_pi_fieldmap select option').each(templateFieldOptions);
           $('#_Inputfield_pi_maptab').click(function() {
             //$('#wrap_Inputfield_iu_fieldmap select option').each(templateFieldOptions);
           });
         } else {
           clearFieldOptions();
         }
       }
       function getTpl() {
         if ($(sel).length>0 && $(sel)[0].value) { var tpl = $(sel)[0].value; }
           else { var tpl = null; }
         return tpl;
       }
       function templateFieldOptions() {
         var val = parseInt(this.value);
         if (!arr.includes(val) && val>0) {
           //console.log(val + ' ' + arr);
           $(this).prop('selected',false);
           $(this).prop('disabled',true);
         } else {
           if (arr.includes(val) && val>0) {
             //console.log(tpl);
             $(this).prop('disabled',false);
           }
         }
       }
       function clearFieldOptions() {
         $('#Inputfield_pi_match').find('option[value=""]').first().prop('selected',true);
         $('#Inputfield_pi_match').find('option[value!=""]').each(function() {
           $(this).prop('disabled',true);
         });
         $('#Inputfield_pi_match').prop('disabled',true);

         $('#wrap_Inputfield_pi_fieldmap select').find('option[value=""]').first().prop('selected',true);
         $('#wrap_Inputfield_pi_fieldmap select').find('option[value!=""]').each(function() {
           $(this).prop('disabled',true);
         });
         $('#wrap_Inputfield_pi_fieldmap select').prop('disabled',true);
       }
     }
   });
   </script>
EOT;

   $event->return = str_replace("</body>", "$js</body>", $event->return);
   //$this->filterFieldFields($event);
  }
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...