Juergen Posted July 15, 2020 Share Posted July 15, 2020 Hello @ all ! Today I want to share another new inputfield with the community! It is called Fieldtype OpeningHours and it is designed to enter one or multiple times per day (especially for company opening times). I know that there is another great fieldtype in the repository (https://modules.processwire.com/modules/fieldtype-business-hours/), but I wanted to create my own with a different UI than the other one. Here is a screencast of what it looks like in action: OpeningHours.mp4 A lot of things going on behind the scenes and I dont want to write it all down here, because you can find the whole information on my Github account. https://github.com/juergenweb/FieldtypeOpeningHours Requirements: PHP >= 8.0 (because it uses union types, I have also tested it with new PHP 8.2) ProcessWire >=3.0.181 If you may find any bugs, have any ideas to improve this fieldtype please report it in my Github repository. Greetings from Austria and have a nice day! CHANGELOG: 21.7.20 Add new option to show (true) or hide (false) days with no opening hours on various methods (please be aware that setting options has been changed - it is recommended to deinstall old version and install this inputfield again) . Take a look at the READ.ME for further instructions. 1.1 Add multilang support for timeformat and add 2 additional Schema.org markup methods UPDATE: 09-06-2023: The module has been added to the module directory and can be downloaded from there after it has been published. 18 Link to comment Share on other sites More sharing options...
bernhard Posted July 16, 2020 Share Posted July 16, 2020 He's on fire ??? On 7/15/2020 at 6:02 PM, Juergen said: If you may find any bugs, have any ideas to improve this fieldtype please report it in my Github repository. Really not a big thing, but how would one remove this item? ? Business hours can be a pain... Have you thought about using a library? eg https://github.com/spatie/opening-hours Maybe you have an idea for a good UI regarding exceptions (like holidays etc)? Very nice UI btw! ? 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 16, 2020 Author Share Posted July 16, 2020 Hi @bernhard Thanks for your feedback! 12 hours ago, bernhard said: Really not a big thing, but how would one remove this item? I will check this and and try to find a solution. ? 12 hours ago, bernhard said: Have you thought about using a library? eg https://github.com/spatie/opening-hours I have worked with this library in the past and it's really cool, but for the moment I have not planned to include a third party lib. The main purpose of this inputfield is to make entering of opening times simple for the user. 12 hours ago, bernhard said: Maybe you have an idea for a good UI regarding exceptions (like holidays etc)? Not really! Holidays are always hassle because they differ from country to country. For the moment you can enter only general opening times for all holidays. Exceptions can be really difficult and I guess there you will need to use Spatie to handle this. Link to comment Share on other sites More sharing options...
Juergen Posted July 16, 2020 Author Share Posted July 16, 2020 2 hours ago, bernhard said: Really not a big thing, but how would one remove this item? ? Issue should be resolved and the file was updated on Github ? 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 17, 2020 Author Share Posted July 17, 2020 Hello @ all I have 2 new methods added which take care of days with same times. It often happens that different days have exactly the same opening times. To prevent writing the same times over and over again, you can combine these days with this 2 new methods. 1) First new method to output a multidim. array print_r($page->fieldname->combinedDays()); You can use this array to create the markup by yourself. 2) Render method to output an unordered list of same opening times echo $page->fieldname->renderCombinedDays(); This method outputs a rendered string like this one: <ul class="uk-list"> <li>Mo, Fr: 08:00 - 16:00</li> <li>Tu, Th: 08:00 - 16:00, 18:00 - 20:00</li> <li>We: 16:00 - 23:05</li> <li>Sa, Su, Ho: closed</li> </ul> As you can see days with same times are combined now. Changes are added to Github now, so please download the files again if you want the latest version.? Best regards 3 Link to comment Share on other sites More sharing options...
Juergen Posted July 19, 2020 Author Share Posted July 19, 2020 New feature added: I have updated the inputfield to support multilanguage timeformats on the frontend. You can set the timeformat of each language in the configuration of the inputfield in the backend (see screencast below). Supports strftime() and date() formats. Languageformat.mp4 Version was bumped from 1.0 to 1.1 on Github. Best regards Jürgen 3 Link to comment Share on other sites More sharing options...
Juergen Posted July 20, 2020 Author Share Posted July 20, 2020 2 new methods for Schema.org markup added: print_r($page->fieldname->getjsonLDTimes()); returns an array like this: Array ( [0] => Mo,Tu,We 08:00-12:00 [1] => Mo,Th 13:00-18:00 [2] => Th 08:00-11:00 ) Can be used to create the markup by yourself or echo $page->fieldname->renderjsonLDTimes(); returns a string like this: "Mo,Tu,We 08:00-12:00", "Mo,Th 13:00-18:00", "Th 08:00-11:00" You can use the second method in schema.org Local Business opening hours as followed: ..... "openingHours": [ "Mo,Th,Sa 11:00-14:30", "Mo,Th 17:00-21:30", "Fr,Sa 17:00-22:00" ], ..... Best regards 5 Link to comment Share on other sites More sharing options...
Matze Posted July 11, 2023 Share Posted July 11, 2023 Hi, when trying to install it throws an error: Is this a known issue? Thanks. 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 11, 2023 Author Share Posted July 11, 2023 Hello @Matze First of all, thanks for reporting this issue. 21 minutes ago, Matze said: Is this a known issue? No it is not. I have tried to reproduce the issue, but (fortunately) the module works as expected on my site. I have done the complete installation process including creation of the OpeningHours input field and saving some values. The error that will be printed is also a little bit strange, because it comes from a default ProcessWire method and not from a method that I have written. This should be the method which leads to the error according to your provided error message on line 60: /** * @param Field $field * @return null User is not allowed to change this fieldtype to another * User is not allowed to change this fieldtype to another */ public function ___getCompatibleFieldtypes(Field $field):null { return null; } It seems that there was going something wrong during the installation process in this case. Could you please uninstall the module and reinstall it once more. It would be also interesting which PHP version do you use and which ProcessWire installation. Please let me know if a new install solves the problem. It is a little bit strange because no one has complained so far concerning problems during the installation process. Best regards 1 Link to comment Share on other sites More sharing options...
Matze Posted July 11, 2023 Share Posted July 11, 2023 Hello Juergen, thank you for you quick response. I'm running ProcessWire version 3.0.210 on PHP Version 8.0.28 As a workaround i tried to delete :null and now it works without any errors as far as i checked. But i guess, this should certainly not be the way to solve the problem. Can it maybe be influenced by other modules? I tried to install on my local as on my staging version and the error occurs on both sites. So possible this is influenced by something else? Thank you Matze 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 11, 2023 Author Share Posted July 11, 2023 Hmmm, what you are describing is the return type (:null). So your PHP version is high enough to understand this. I am not sure, but I guess it was introduced in PHP 7. So it makes no sense, that the parser complains about this. The method itself returns the correct type (null) too, so everything is ok. public function ___getCompatibleFieldtypes(Field $field):null { return null; // returns null as expected } I am pretty sure, that other modules do not conflict with this method, because it will be used in a lot of fieldtypes. What you can try is if it allows ": string|null" instead of ":null" (makes no sense, but only for testing purposes). BTW: I have removed the return type on GitHub, so it will not affect upcoming updates. Jürgen Link to comment Share on other sites More sharing options...
Juergen Posted July 11, 2023 Author Share Posted July 11, 2023 Ok, I guess I found the issue...... PHP 8.2: The types null and false can now be used standalone. Take a look here https://www.php.net/manual/en/language.types.declarations.php I am running on PHP 8.2 and therefore I got no problems, so removing the return type on GitHub removes this error message on lower PHP versions. 25 minutes ago, Juergen said: ": string|null" So this should work, because null is not standalone.? Anyway, I am wondering a little bit that no others discover this issue, because a lot of users are running lower PHP version than 8.2. Best regards Jürgen 1 Link to comment Share on other sites More sharing options...
Matze Posted July 11, 2023 Share Posted July 11, 2023 Ah ok, thank you very much. I need to update my environment to PHP 8.2., too. PS: I love the module! (even if imho the time input field is a bit tricky to handle, but thats a browser thing) 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 11, 2023 Author Share Posted July 11, 2023 10 minutes ago, Matze said: I need to update my environment to PHP 8.2., too. I guess PHP 8.3 coming out in autumn ? 11 minutes ago, Matze said: the time input field is a bit tricky to handle, but thats a browser thing I know, but usually you do not enter opening times very often ? 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted July 11, 2023 Share Posted July 11, 2023 I just started playing around with this module, and so far it's really nice. There is just one thing that is a bit weird for me, as I don't really need that holiday entry. Is there a way to hide it? Sure, I could use showClosed=false, yet I'd like to list all days of the week, as it makes it way easier to read and compare. Another minor thing in regards to that holiday option that came up while playing around... would it be possible to somehow add a label to it and add up to 5 or more entries? For example, if I wanted to add Christmas Eve, the 1st and 2nd Christmas Day, New Year's Eve, and New Year's Day, That would be 5 days for which I would either need a date or a label, so everyone knows exactly what days are meant. 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 11, 2023 Author Share Posted July 11, 2023 Hello @wbmnfktr Thanks for your post! 1 hour ago, wbmnfktr said: There is just one thing that is a bit weird for me, as I don't really need that holiday entry. Is there a way to hide it? No, not at the moment, but this seems to be a useful feature to implement. I will try to add a new field to the field configuration, where you can choose, if you want to display the holiday field or not.? 1 hour ago, wbmnfktr said: Another minor thing in regards to that holiday option that came up while playing around... would it be possible to somehow add a label to it and add up to 5 or more entries? This is where the things starts going complicated - I guess this is not really easy to implement. You have to create a field containing all exceptions. This is not what can be achieved very easily, so I guess this feature will not be realized. Best regards 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted July 12, 2023 Share Posted July 12, 2023 4 hours ago, Juergen said: No, not at the moment, but this seems to be a useful feature to implement. I will try to add a new field to the field configuration, where you can choose, if you want to display the holiday field or not.? This would be awesome! 4 hours ago, Juergen said: This is where the things starts going complicated I was afraid you would say that, yet I almost knew it. On the other hand I wanted to mention such an addition for your fieldtype. Since my last post I tried a few things more and there is another thing I found... would it be somehow possible to wrap days and times in individual <span>s or define a dl instead of an ul? My goal is to make it look like a table, which is not possible right now - besides using the array version, which isn't that handy - but would work probably. On the other hand... your JSONLD output example is absolutely perfect for what I can tell right now. 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 12, 2023 Author Share Posted July 12, 2023 4 hours ago, wbmnfktr said: Since my last post I tried a few things more and there is another thing I found... would it be somehow possible to wrap days and times in individual <span>s or define a dl instead of an ul? Please be patient - I will add surrounding tags for the dayname and the time(s) for that day, so you will be more flexible. The tags will be variable, so you can choose if you want fe dt and dd or div and span or whatever. The other tags (ul and li) should be also flexible, so you could change them to your needs. Every tag should also be disabled by adding false instead of a tag name - thats my plan. Maybe I can bump up the version with the changes today - we will see ? 1 Link to comment Share on other sites More sharing options...
Juergen Posted July 13, 2023 Author Share Posted July 13, 2023 Version 1.1 out now! This version includes feature requests according to mark-up rendering and a new field configuration to show/hide Holiday. As always, please test it carefully after update and report problems. You will find the full list of changes here: CHANGELOG.md Please read it carefully to see, what is new 2 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted July 16, 2023 Share Posted July 16, 2023 Short follow-up: it's perfect now! 1 Link to comment Share on other sites More sharing options...
Matze Posted July 19, 2023 Share Posted July 19, 2023 Is there an easy way to check if at least one time is set? if($page->MyOpeningTimes) { // always active } Link to comment Share on other sites More sharing options...
Juergen Posted July 19, 2023 Author Share Posted July 19, 2023 Hello @Matze No, not by default, but I have added a new method which counts the times set: getNumberOfTimes() You can use this method to do a check, but you have to update to version 1.2.0. Best regards Jürgen 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