Jump to content

bytesource

Members
  • Posts

    220
  • Joined

  • Last visited

Everything posted by bytesource

  1. Adrian, My code above only works for pages that have already been saved to the database and therefore have a page id. When used with the title tag, however, this is a problem, as the value for this field must be set before the page is saved. As it turns out you already anticipated this use case by calculating the unique value based on the id of the last page saved to the database: http://processwire.com/talk/topic/4201-how-to-automatically-set-the-value-of-a-field/#entry41172 I marked your answer as the best answer, so that others will find your solution faster than I did. Cheers, Stefan
  2. Adrian, You are right, I missed the fact that you changed the after hook into a before hook. I edited my code accordingly and am glad to announce that now everything is working as exptected: class FieldHelper extends WireData implements Module { /** * getModuleInfo is a module required by all modules to tell ProcessWire about them * * @return array * */ public static function getModuleInfo() { return array( 'title' => 'FieldHelper', 'version' => 100, 'summary' => 'Hide fields depending on permission, role, or user.', 'singular' => true, 'autoload' => true ); } public function init() { // add before-hook to the inputfield render method $this->addHooKBefore("Inputfield::render", $this, "renderField"); } /** * If not empty, create unique value for textfield 'invoice_number' * */ public function renderField(HookEvent $event) { $field = $event->object; if($field->name == 'invoice_number' && $field->value == '' ) { // $id is unique, as page id is unique $id = date('Y') . "/" . $this->process->getPage()->id; $field->set('value', $id); } } } Thanks a lot for your help! Cheers, Stefan
  3. I apologize for replying late. Unfortunately this happens quite often due to the time difference. I wrote that I wanted to make the title field guaranteed to be unique, but used the custom field invoice_number instead as I already created some pages and did not want to mess with existing titles before being sure the module code worked. So, yes, ultimately it is about uniqueness, and in this regard Ryan's fieldtype Text Unique might in fact come in handy (thanks for the tip, Host). However, I also always try to learn something new, and right now I would like to find out how to dynamically set a value for a field - any value would be fine at this point. But even when I set the value 'hello' for the value of the field: $field->set('value', "hello"); I did not get any output. So I guess the problem might be with one of the following: Incorrect usage of either $field->get() or $field->set() or both. Some template or field role restriction that resulted in this error message: This webpage has disabled automatic filling for this form. I haven't edited any roles, though. Cheers, Stefan
  4. Hi all, I am trying to build a very simple invoice system using Processwire. There is one invoice per page. To ensure that each page has a unique title, I would like to set the value of the title field automatically. I rewrote a module I found on the forum, but the following code does not work (a blank field stays always blank in the admin): public function init() { // add after-hook to the inputfield render method $this->addHookAfter("Inputfield::render", $this, "renderField"); } public function renderField(HookEvent $event) { // // get the current field $field = $event->object; if($field->name == 'invoice_number' && $field->get('value') == '' ) { $id = date("Y") . "/" . $page->id; // example for a possible id value $field->set('value', $id); } } Also, when clicking on the empty input field, the following message is displayed: This webpage has disabled automatic filling for this form. I now have the following questions: Is it even possible to automatically set the value of a field? If yes, how could I change the above code so that it works as intended? Cheers, Stefan
  5. Hi Ryan, Multi-language fields are indeed very pleasant to work with, and I try to manage most of the site's content using these fields. My question therefore only refered to static text in templates. You are right, though, eternal tweaking of such content is certainly not the best way to go Cheers, Stefan
  6. @WillyC I did not know about html_entity_decode(), and in fact is solves my problem for now: <p> <?php $t = "We love to <span class='highlight'>highlight</span> so that it <span class='highlight'> stands out</span>."; $t = __($t); echo html_entity_decode($t, ENT_QUOTES, 'UTF-8'); ?></p> main.css .highlight { font-weight: 600; color: red; } The string will be output with the words 'highlight' and 'stands out' shown in bold red. Thanks a lot for this tip! At everyone: Soma mentioned that when using __("text"), everytime I change the text, the translations needed to be updated, too. I haven't started doing any translations, but I know that I often tweak text here and there, even if I intially thought it was 'final'. I certainly don't want the translaters have to redo their translation every time I do such a tweak. Is there a way to deal with edits to the main language text without effecting its translations? Cheers, Stefan
  7. Hi Soma, That is a very interesting option. It's just that I haven't dealt with translations before, and therefore am not quite sure how to proceed. Could you post a link to an introduction of such translation system? Cheers, Stefan
  8. Hi Horst, Thanks for pointing me to the code handling the translation. Splitting the text at every tag is a bit cumbersome and might make sentences more difficult to translate, when words get out of context, but this is only a small inconvenience compared to the advantage of having built-in multi-language support. Cheers, Stefan
  9. Hi all, I was wondering about the best practices for translating text that contains HTML tags such as span. Example: <p> We love to <span class='highlight'>highlight</span>so that it <span class='highlight'>stands out</span>. </p> The following does not work as the tags won't be interpreted but output as is: <p> <?php echo __("We love to <span class='highlight'>highlight</span>so that it <span class='highlight'>stands out</span>."); ?> </p> Cheers, Stefan
  10. Hi teppo, Thanks for pointing me to the permissions. I found that for some reason errors.txt was not part of the www-data group. After fixing that problem, errors got logged again. Thanks again for your help! Cheers, Stefan
  11. Hi all, When I checked my log file at site/assets/logs/errors.txt I realized that the last entry was from months ago. Is there anything I can do to have all errors get logged? Cheers, Stefan
  12. @kongondo It did not know the difference between $page and wire('page). Only a few days ago I ran into a situation where $page was not recognized. Now I think using wire('page') instead would have done the trick. Cheers, Stefan
  13. I updated the code and now everything is working: public function renderField(HookEvent $event) { // get the current field $field = $event->object; // example 1permission if($field->name == "product_category") { if($this->process->getPage()->template == 'offer') { $event->return = ''; } } } Soma, thanks a lot for your help! Cheers, Stefan
  14. @Soma I want to hide a field based on the id of the parent page, whoever the following does not work: public function renderField(HookEvent $event) { // get the current field $field = $event->object; if($field->name == "product_category") { if($page->parent->id == 6) { $event->return = ''; } } } I guess my question is: How can I refer to the current page from within a module? Cheers, Stefan
  15. That works great, thanks a lot! Your solution finally made me realize that we are not only dealing with pages but also with different field types. Cheers, Stefan
  16. @Wanze I tried your code, but $image->type does not produce any output. I have to admint I am not quite sure what FieldtypeCropImage stands for. As far as I understand $image is still an instance of PageImage, with a dynamically added 'getThumb' method. Indeed, the following always returns 'original image' if ($image instanceof PageImage) echo "original image"; else echo "thumbnail"; // => original image @ryan I like your second suggestion. It works perfectly. Cheers, Stefan
  17. Hi, I wrote a helper function that takes an image field instance and returns its respective <img> ... /> HTML. After installing the Thumbnail module I wanted to always output the thumbnail if present. In order to determine of an image has an additional thumbnail, I tested for the 'getThumb' method that is added by the module to PageImage. However, the following code always returned the path to the original image, even if a thumbnail was present: $url = method_exists($image, 'getThumb') ? $image->getThumb('thumbnail') : $image->url; This leads me to the following questions: Why does the above code not work? Is there a better way to differentiate between a 'normal' image field and one that can contain a thumbnail? Cheers, Stefan
  18. Hi Ollie, I am not using the Processwire minify module, but this solution: https://github.com/mrclay/minify In the HTML the different javascript files need to be output like this to trigger the minification (works also with CSS files): <script type='text/javascript' src='/min/?b=sovonex/site/templates/js&f=jquery-1.9.1.min.js,tabs-accordion/index.js,tabs-accordion/jquery.ba-resize.js,tabs-accordion/tabs-accordion.js,jquery.placeholder.min.js,include.js'></script> Cheers, Stefan
  19. @Dave Thanks for pointing me to IETester! That program will save me a lot of trouble. @Horst Thanks for sending me all these screenshots! The only problem I noticed was that the third level menu item did not appear on the right but below the second level menu items (please see the attached screenshot). You both really helped me a lot! Cheers, Stefan
  20. Hi, I build this site template on Themeforest and am half-through porting my business site to this theme. The theme should work with IE8, but when I tried to open the site on a Windows machine in IE8 today, the javascript did not render at all. As this might be due to the Chinese Internet blocking some content, I would be glad if someone could test the site in IE8 (and maybe IE7, but that probably won't work) on his or her computer. Cheers, Stefan
  21. That works brilliantly: $config->minify = new FilenameArray(); $config->minify->add("test.js"); $config->minify->add("test2.js"); $config->minify->add("test.js"); foreach($config->minify as $min) echo $min . ' '; // => test.js test2.js Thanks a lot! That is exactly what I wanted to achieve. Cheers, Stefan
  22. I have one additional question: Is there a way to add a variable to $config that behaves like $config->styles? Example: $config->styles->add('default.js'); $config->styles->add('custom.js'); $config->styles->add('custom.js'); $config->styles->add('custom.js'); foreach($config->styles as $style) echo $style . ' '; // => default.js custom.js // no duplicates returned Cheers, Stefan
  23. @diogo That is exactly what I was looking for. @Soma $config is WireDate, now it all makes sense. diogo, Somo, thanks a lot for your help! Cheers, Stefan
  24. Hi Horst, Your suggested approach does indeed work: $config->minify = array( 'hello', 'world'); echo count($config->minify); // => 2 However, I would like to start with an empty array and add items dynamically. This does not seem to work with $config. Also, I would like to take a look at the implementation of $config, but couldn't find any automatically created API documentation of all Processwire objects. Cheers, Stefan
  25. I want to add an array variable to $config to store file paths, but is does not seem to work: $config->minify = array(); $config->minify[] = 'hello'; $config->minify[] = 'world'; echo count($config->minify); // => 0 On the other hand, adding a string works as expected: $config->minify = ''; $config->minify .= 'hello'; $config->minify .= 'world'; echo $config->minify; // => helloworld I am still new to PHP I am probably not seeing the obvious, so I would be glad if someone could point me in the right direction. Best regards, Stefan
×
×
  • Create New...