-
Posts
5,034 -
Joined
-
Days Won
340
Everything posted by Robin S
-
Using the brand new "multiple methods" hook feature in PW 3.0.137 you could save the average stars value to a decimal (or float) field every time a comment is approved/disapproved/updated/deleted. Then you can find or sort pages by the field more efficiently. The hook below assumes a comments field named "comments" and a decimal field named "decimal": $wire->addHookAfter('FieldtypeComments::commentApproved, FieldtypeComments::commentUnapproved, FieldtypeComments::commentDeleted, FieldtypeComments::updateComment', function(HookEvent $event) { /* @var Page $page */ $page = $event->arguments(0); /* @var Field $field */ $field = $event->arguments(1); if($field->name !== 'comments') return; if($page->template->hasField('decimal')) { // Save the average star rating for comments with approved or featured status $page->setAndSave('decimal', $page->comments->find("status=1|2")->stars(true)); } }); P.S. You'd do a one-off API operation to save the average stars value for all your existing comments to get things set up initially.
-
Here's a slightly different approach. Assumes an image field named "images" and a CKEditor field named "body". foreach($page->images as $image) { // Get the image URL excluding the file extension (to account for image variations) $match_url = substr($image->url, 0, -strlen($image->ext)); // Skip the image if the match URL occurs at the start of a src attribute in $page->body if(strpos($page->body, "src=\"$match_url") !== false) continue; // Output the image or whatever echo "<img src='{$image->width(300)->url}' alt='$image->description'>"; }
-
Datepicker Year Range not Displaying the Past Years
Robin S replied to lenoir's topic in General Support
It's working as expected for me. You're not including the quote marks around the value by any chance are you? If -10:+10 is actually the value you want you can leave the setting empty because that is the default. -
As stated in the module readme, FieldtypeYaml uses a namespaced version of Spyc. And the examples for Spyc show how to dump an array to YAML: https://github.com/mustangostang/spyc/blob/master/examples/yaml-dump.php So to reproduce that example using the namespaced Spyc bundled with FieldtypeYaml: // Init the module so Spyc gets loaded $modules->get('FieldtypeYaml'); // Some array data $array[] = 'Sequence item'; $array['The Key'] = 'Mapped value'; $array[] = array('A sequence','of a sequence'); $array[] = array('first' => 'A sequence','second' => 'of mapped values'); $array['Mapped'] = array('A sequence','which is mapped'); $array['A Note'] = 'What if your text is too long?'; $array['Another Note'] = 'If that is the case, the dumper will probably fold your text by using a block. Kinda like this.'; $array['The trick?'] = 'The trick is that we overrode the default indent, 2, to 4 and the default wordwrap, 40, to 60.'; $array['Old Dog'] = "And if you want\n to preserve line breaks, \ngo ahead!"; $array['key:withcolon'] = "Should support this to"; // Dump to YAML with some (optional) custom indent and wordwrap settings $yaml = \owzim\FieldtypeYaml\Vendor\Spyc::YAMLDump($array,4,60); // See the result in a Tracy dump bdb($yaml, 'yaml'); You can convert a WireArray to a plain array with getArray().
-
How to detect if a template form has errors in it?
Robin S replied to VeiJari's topic in API & Templates
There are a couple of different questions here. Check if saved page has errors: $wire->addHookAfter('Pages::saveReady', function(HookEvent $event) { /* @var Page $page */ $page = $event->arguments(0); // Find out if the page being saved has any errors $has_errors = $page->hasStatus(Page::statusFlagged); // Do something accordingly... }); You can set custom error messages as follows. This is for a text inputfield - adjust to suit the type of inputfield in question: $wire->addHookAfter('InputfieldText::processInput', function(HookEvent $event) { /* @var InputfieldText $inputfield */ $inputfield = $event->object; $field = $inputfield->hasField; // Return early if the field name doesn't match if(!$field || $field->name != 'text_1') return; // If some condition is or isn't met if($inputfield->value && $inputfield->value !== 'foo') { // Show an error for the inputfield $inputfield->error('The value of this field must be empty or foo'); // Maybe clear the inputfield value $inputfield->value = ''; } }); -
URL character limit is 128 - how to increase this?
Robin S replied to formulate's topic in General Support
Not sure why this means URL segments can't be a solution. You hook Pages::added and put the title through $sanitizer->pageName and save it to a hidden field (full_page_name). Then in the parent page template you check the URL segment against full_page_name and render any matching page (and throw a 404 if none found). For URLs to the pages you hook Page::path as per Ryan's case study and concatenate full_page_name to the parent URL. -
If you are looping over the whole PageArray then the sort position is the key (zero indexed). foreach($items as $key => $item) { echo "$item->title - sort position is $key"; } If you want to get individual pages from the PageArray and know their sort position you can first loop over the PageArray and save the sort position to a custom property on each page. foreach($items as $key => $item) { $item->sort_position = $key; } $one = $items->get("foo=bar"); echo "$one->title - sort position is $one->sort_position";
-
@gmclelland, thanks for the report, should be fixed in v0.1.1.
-
[Solved] MarkupPagerNav always shows same results
Robin S replied to rick's topic in General Support
Okay, I see where you are going wrong. You need to enable pagination for the template that is listing the results, not for the template of pages you are getting in your find() selector. -
[Solved] MarkupPagerNav always shows same results
Robin S replied to rick's topic in General Support
Are you 100% sure you have "Allow Page Numbers" checked in the template settings? Your links should be like "/page2/". A link like "?page=2" and the same results on every pagination is usually a sign that page numbers have not been enabled for the template. -
Thanks for taking on this module @teppo. A couple of minor styling observations with the Uikit theme... The spacing around the buttons is a little inconsistent. And to my eye the triangle that indicates the active button feels like it's off-centre, because the icon feels like it is separate to the word rather than them both making up a single visual unit. So for the Uikit theme I'd be inclined to centre the active marker on the word alone rather on icon + word. For pages where some buttons are not available... When "Edit" is not available the button text changes (the full stop looks out of place here), but when "New" is not available the button text is gone but the icon remains. Not sure but maybe unavailable buttons should not render at all?
-
Adding data- attributes (with no value) to form doesn't work
Robin S replied to awesomolocity's topic in General Support
You could update to the latest dev. Support for empty data attributes was added in this commit. -
of() is a method of Page objects, but you're trying to use it on an PageArray of Repeater pages. Try: // Get the page you want to work with $home = $pages->get("template=home"); // Turn output formatting off for the page $home->of(false); // Change the Repeater field value $home->submissions->removeAll(); // Save the page $home->save();
-
Filtering pages where field value is greater than a negative number
Robin S replied to cst989's topic in General Support
From past experience working with lat/lng coordinates, I suggest using a decimal field for these. Float fields only have a precision of 6 figures which is often not sufficient for a lat/lng value. -
Image management in custom module
Robin S replied to SoccerGuy3's topic in Module/Plugin Development
Does the module need to read data back from the external database and display it in an interface for editing, or is PW only used for creating new data and inserting it into the database? Because if it's the latter you could still use Page Edit as the interface for data creation. You'd use a saveReady hook to get, prep and insert the data from the page into the external database, then clear out the page ready for new data. Maybe you'd add an extra submit button to Page Edit ("Send to database") so that the normal save buttons can be used to temporarily save data locally and only the "Send" button finally inserts and clears the data when the user is ready. -
A client hired a security consultant to do a site analysis and they advised that the X-Content-Type-Options HTTP header should be set to "nosniff". The MDN docs for this header say: "Site security testers usually expect this header to be set." https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options This was easily resolved by adding the following to .htaccess Header set X-Content-Type-Options "nosniff" Do you think it would be good to add this to the default PW .htaccess file?
- 1 reply
-
- 4
-
-
Image management in custom module
Robin S replied to SoccerGuy3's topic in Module/Plugin Development
Why not use pages and proper Fieldtype fields to store the data? Seems like it would be much easier, and anything out-of-the-ordinary that you want to show within your Process module you could show within Page Edit by hooking the render of a markup inputfield, or by using one of the runtime field modules from kongondo, bernhard, kixe, or me. -
How to pass all API variables to $files->render() ?
Robin S replied to bernhard's topic in General Support
From the $files->render() docs: -
$pages->find() get by id and maintain order
Robin S replied to Torsten Baldes's topic in API & Templates
There is some code and some links to explore in this post: -
Try: $wire->addHookAfter('Field::getInputfield', function(HookEvent $event) { $page = $event->arguments(0); $inputfield = $event->return; // Only for non-superusers if($event->wire('user')->isSuperuser()) return; // Only for a particular Repeater page ID if($page->id !== 1553) return; // Set collapsed to Inputfield::collapsedNoLocked or Inputfield::collapsedHidden as suits $inputfield->collapsed = Inputfield::collapsedNoLocked; });
-
Get page with a specific template of root parent
Robin S replied to Stefanowitsch's topic in API & Templates
When you do this you load all the children of the of the root parent into memory as a PageArray, and then you get just one of those pages. It's more efficient to directly get the single page you need: $page->rootParent()->child('template=menu_submenus, include=hidden'); -
I suspect the image is corrupt in some way, or maybe contains some metadata, colour profile, etc, that can't be handled. Try opening this image in Photoshop or similar and then save it as a new JPG, making sure to use the standard sRGB colour profile.
-
When you add an image via the API rather than via the inputfield you have to do all the work yourself that the inputfield would otherwise do for you. A recent answer to a similar question, which contains a link to some code that might be useful to you:
-
v0.1.4 released. This version adds support for some new features added in Repeater Matrix v0.0.5 - you can disable the settings for matrix types (so items cannot change their matrix type), and when the limit for a type is reached it becomes unavailable for selection in the type dropdown of other matrix items. The module now requires Repeater Matrix >= v0.0.5. If you are using Repeater Matrix v0.0.4 or older then there is no need to upgrade Restrict Repeater Matrix as the new features in v0.1.4 are only applicable to Repeater Matrix v0.0.5.