Jump to content

Hook to add custom CSS & JS on ProcessPageEdit of particular template via module? and filter options displayed


hellomoto
 Share

Recommended Posts

I have a module with 'autoload' => 'template=admin'. I need to hook into ProcessPageEdit for editing template=importupdate, and within my hook function add CSS and JS files. I have:

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

private function addlStyles(HookEvent $event) {
	//$page = $event->arguments(0); if($page->template != 'importupdate') return;
	$css = wire('config')->urls->modules . 'ImportUpdateUltimate/editImportUpdate.css';
	$this->config->styles->add($css);
}

which does nothing. 

  • Like 1
Link to comment
Share on other sites

Done did it:

public function init() {
	$this->addHookAfter('Page::render', $this, 'styling');
	$this->addHookAfter('Page::render', $this, 'filterFieldFields');
}

  protected function styling(HookEvent $event) {
   $page = $event->object; $edid = $this->pages->get($this->input->get->id);
   if(!($page->process = 'ProcessPageEdit' && $edid->template = 'importupdate')) return;
   $css = wire('config')->urls->ImportUpdateUltimate . 'ass/edit_importupdate.css';
   $event->return = str_replace("</head>", "<link rel='stylesheet' type='text/css' href='{$css}'/>\n</head>", $event->return);
  }

  protected function filterFieldFields(Hookevent $event) {
    $page = $event->object; $edid = $this->pages->get($this->input->get->id);
    if(!($page->process = 'ProcessPageEdit' && $edid->template = 'importupdate')) return;
    if($edid->iu_template) $tpl = wire('templates')->get((int) $edid->iu_template[0]);
    else $tpl = null;
    $fields = [];
    if($tpl != null) {
      foreach ($tpl->fields as $f) { $fields[] = $f->id; }
      $fstring = '[' . implode(',',$fields) . ']';
    } else $fstring = '[]';
    $js = <<<EOT
    <script>
    $(document).ready(function(){
      var arr = $fstring;
      var def = '<option value=""></option>';

      if (arr.length) {
        $('#Inputfield_iu_match option').each(templateFieldOptions);
        $('#wrap_Inputfield_iu_fieldmap select option').each(templateFieldOptions);
        $('#_Inputfield_iu_maptab').click(function() {
          $('#wrap_Inputfield_iu_fieldmap select option').each(templateFieldOptions);
        });
      } else { clearFieldOptions(); }

      $('#wrap_Inputfield_iu_template .asmListItemRemove').click(clearFieldOptions);

      function templateFieldOptions() {
        var val = parseInt(this.value); //console.log(typeof(val));
        if (!arr.includes(val) && val>0) {
          //console.log(val);
          $(this).prop('selected',false);
          $(this).prop('disabled',true).remove();
        }
      }
      function clearFieldOptions() {
        $('#Inputfield_iu_match').html(def);
        $('#wrap_Inputfield_iu_fieldmap select').each(function() {
          $(this).html(def);
        });
      }
    });
    </script>
EOT;
    $event->return = str_replace("</body>", "{$js}</body>", $event->return);
  }

 

  • Like 4
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...