Jump to content

sforsman

Members
  • Posts

    131
  • Joined

  • Last visited

  • Days Won

    5

Community Answers

  1. sforsman's post in Adding several items to a PageArray was marked as the answer   
    I know you said you didn't want to use a selector, but I'm just wondering why on earth? You are effectively doing the same as
    $items = $pages->find('name=page-1|page-2|page-3|page-4|page-5, parent=/'); But your code will just execute slower.
  2. sforsman's post in Inputfield Dependencies parent_id was marked as the answer   
    This is not a problem for ProcessWire. There would be multiple ways of implementing that. Check this tutorial for an example
    https://processwire.com/talk/topic/7548-dual-url-structure-for-categorized-content
  3. sforsman's post in Upgrade was marked as the answer   
    Do you mean this was just a one time error? Then there is no problem at all! WireCache is initialized and used in Modules initialization before SystemUpdater, so on the first request after the upgrade you would get an error like that.
  4. sforsman's post in Situation where I need set a value for a page selected from a pagefield was marked as the answer   
    Hello Gabe and welcome,
    Are we talking about a one single template called material here? Are all of the characteristics usually defined for all of the materials? And the problem is that you don't want to give your client access to the templates and fields of the system?
    If the answer to all questions is 'yes', then I think approaching the problem with autoloading modules, hooks, PageTables etc is a bit overkill. It is amazingly simple to create a Process-module that you can give the client access to, which simply allows the client to add/edit/remove such fields on the material-template. Here are all the relevant API-methods
    $template = wire('templates')->get('material'); // Adding a field $field = new Field(); $field->name = "c_durability"; $field->label = "Durability"; $field->type = "Integer"; $field->save(); $template->fields->add($field); $template->save('fields'); // Updating the label $field = wire('fields')->get('c_durability'); $field->label = "Characteristics: Durability"; $field->save(); // Removing the field $field = wire('fields')->get('c_durability'); $fieldGroups = $field->getFieldgroups(); if($fieldGroups->count() > 0) { foreach($fieldGroups as $fieldGroup) { $fieldGroup->remove($field); $fieldGroup->save(); } } wire('fields')->delete($field); Now of course you still need an UI with some confirmation dialogs and validation, but I'm sure you get the idea (if not, ask away).
    If you really, really want to create a module that does this automatically based on pages, then yes, that can be done with the same methods as well. Such module could be something like this
    class PageFieldCreator extends WireData implements Module { protected $material_template = "material"; protected $char_template = "characteristic"; protected $template; public static function getModuleInfo() { return Array( 'title' => __('Page Field Creator', __FILE__), 'summary' => __('Demo-module that creates fields from pages'), 'version' => 1, 'singular' => true, 'autoload' => true, ); } public function init() { $this->addHookAfter('Pages::saved', $this, 'saveHook'); } public function saveHook(HookEvent $e) { $page = $e->argument(0); if($page->template->name != $this->char_template) return; if($page->parent->isTrash()) $this->handleDelete($page); else $this->handleSave($page); } protected function handleDelete(Page $page) { $fieldname = $this->getFieldname($page); $field = $this->fields->get($fieldname); if(!$field) return; $fieldGroups = $field->getFieldgroups(); if($fieldGroups->count() > 0) { foreach($fieldGroups as $fieldGroup) { $fieldGroup->remove($field); $fieldGroup->save(); } } $this->fields->delete($field); $this->message(__("Characteristic {$fieldname} deleted")); } protected function handleSave(Page $page) { $fieldname = $this->getFieldname($page); $field = $this->fields->get($fieldname); if(!$field) $this->handleNew($page); else $this->handleUpdate($field,$page); } protected function handleUpdate(Field $field, Page $page) { if(strcmp($field->label, $page->title) == 0) return; $field->label = $page->title; $field->save(); $this->message(__("Characteristic {$field->name} updated")); } protected function handleNew(Page $page) { $field = new Field(); $field->name = $this->getFieldname($page); $field->label = $page->title; $field->type = "Integer"; $field->save(); $tpl = $this->getTemplate(); $tpl->fields->add($field); $tpl->save('fields'); $this->message(__("Characteristic {$field->name} created")); } protected function getFieldname(Page $page) { return "c_".$this->sanitizer->fieldName($page->name); } protected function getTemplate() { if(!isset($this->template)) $this->template = $this->templates->get($this->material_template); return $this->template; } } Such module would create/update/delete a field and update the template called material when a page that has the template characteristic is created/updated/trashed. The demo creates the field using the name of the page prefixed with "c_".
    While this would technically work, I'd still just create a Process-module for managing them to gain more fine-tuned control. Like I mentioned in the beginning, hooking is also a bit overkill since the fields are rarely modified.
  5. sforsman's post in Upgrade to 2.5 internal (500) server error was marked as the answer   
    I have some questions but first I'd like you to enable debug-mode in your site/config.php ($config->debug), then try again and post us the result. If you still get the same page (which I'm hoping you don't), could you answer these questions
    Are you able to access the web server's error log? If yes, please post the error here Do you have something in site/assets/logs/error.txt? If yes, please post the error here Do you get that on the frontend of your site, or when trying to access the admin-site? What modules do you have under site/modules? Could you post us the result of <?php print_r(get_loaded_extensions()); ?> (or `php -m` if you can access a shell)? 
  6. sforsman's post in Managing a Page's files by filename was marked as the answer   
    Hey! Sorry for the late response - I've been fairly busy lately. First of all, no, I don't think it's documented anywhere else but here: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/Pagefile.php#L441-L444
    The reason for that probably is because it's very rarely needed.
    I'm glad you worked it out. However I would like to mention that you should rather do 
    // Process the upload only once $filenames = $u->execute(); if(count($filenames)) { // If there's always just one file, no looping needed $pagefile = new Pagefile($p->files, $filenames[0]); $p->files->add($pagefile); $p->save(); echo $pagefile->hash; } You don't need to use the last() -trick - internally ProcessWire is changing the string you are passing to add() to a Pagefile object anyway.
    Also I assume you haven't enabled zip-file extraction? Because then you should handle all the files returned from the execute() - then you would also have to separate the hashes with something you can parse.
  7. sforsman's post in Renaming the default site directory was marked as the answer   
    You shouldn't edit index.php directly. Instead you should create a file called index.config.php and put the following there.
    <?php if(!defined("PROCESSWIRE")) die(); function ProcessWireHostSiteConfig() { return ["*"=>"myNewSiteLocation"]; } Obviously change the string myNewSiteLocation.
    Edit: Forgot to mention that if your directory name doesn't start with "site-", you should create your own .htaccess inside your site-directory that protects assets in the same way the main .htaccess does. The main .htaccess protects only the default site folder and folders starting with "site-". You could modify the main .htaccess too, but again you should avoid editing it to make upgrading PW easier.
×
×
  • Create New...