Jump to content

MarkE

Members
  • Posts

    921
  • Joined

  • Last visited

  • Days Won

    10

Posts posted by MarkE

  1. Something like this hook in your ready.php file?

    wire()->addHookAfter('InputfieldSelect::render', function($event) {
    	$inputfield = $event->object;
    	if($inputfield->name == 'my_select_field') {
    		if(count($inputfield->options) == 1) {
    			$inputfield->description = "text for one item";
    			$event->object = $inputfield;
    			$event->return = "<p>{$inputfield->options[1]}</p>";
    		} else if(count($inputfield->options) == 0) {
    			$event->return = "<p>No options!</p>";
    		}
    	}
    });

    EDIT: Added condition so that it only operates on the relevant inputfield. Also, note that while this fixes the UI, you will need to make sure the value is provided where needed as it is no longer selected (may be possible to address this by making the value required and providing the default value).

    EDIT2: For example, you could set the value like this:

    $page->of(false);
    $inputfield = $fields->get('my_select_field')->getInputfield($page);
    if(count($inputfield->options) == 1) {
        $page->test_select = $inputfield->options[1];
        $page->save('my_select_field');
    }

    Is that any help @The G?

  2. 8 hours ago, rastographics said:

    The next thing I might try is to open my project completely from the WSL2 environment and see if that can give me the same great performance instead of mutagen.

    I now have my project files in the WSL2 environment and that does seem to improve things without mutagen. I also tried running PhpStorm in WSL2 but it was a bit of a pain and no quicker. 

  3. Thanks for all the ideas @Jan Romero . Personally I got a bit frustrated with TrackChanges because it seems to be a bit random about what it records - for pages as well as fields etc. That’s why I started using getFresh() for pages with a home-built compare function because it gave me full control and understanding. So my idea was to do something similar for fields and templates (and fieldgroups) - the code above is my attempt at this and no doubt can be improved. The getFresh() for pages is hugely more complex!

    Nevertheless, I agree that a more robust and consistent TrackChanges is desirable. 

  4. 1 hour ago, Jan Romero said:

    I just tested this:

    That seems to work and might be sufficient if you just want to know if there are any changes. The test that I did - adding a field to a template - gave a curious array of changes in the bar dump, viz:

    array
    'compile' => array
    	0 => 3
    'noParents' => array
    	0 => ''

    i.e. nothing about the field that had been added. I have no idea why this is.

    On the other hand, my getFreshSaveableItem() method returns the complete template before any changes, so includes the related fieldgroup. However it may be slower - I haven't benchmarked them.

  5. So here's a (hopefully better and working) solution. Add the following as a hook to (i.e. new method for) WireSaveableItems:

    	public function getFreshSaveableItem($event) {
    		$saveables = $event->object;
    		$item = $event->arguments(0);
    		$database = $this->wire()->database;
    		$sql = $saveables->getLoadQuery()->getQuery();
    		$query = $database->prepare($sql);
    		$query->execute();
    		$rows = $query->fetchAll(\PDO::FETCH_ASSOC);
    		$newItem = null;
    		if($item) {
    			foreach ($rows as $key => $val) {
    				if ($val['id'] == $item->id) {
    					$row = $rows[$key];
    					$newItem = $saveables->initItem($row);  // there should be only one matching item
    					break;
    				}
    			}
    		}
    		$event->return = $newItem;
    	}

    This returns the item as it is in the database, so if you call (say) wire('fields')->getFreshSaveableItem($myfield) in a beforeSave hook, you will get the version of $myfield that is in the database - i.e before it is saved and can compare it with the version about to be saved.

    But if you want to use this, test it out well first!

    Maybe someone else can come up with something better (or even a PR for the core?!)

  6. What software do people like for showing off their modules, websites etc.? I have tried the (free version of) screencast-o-matic and Clipchamp but neither really suits me. The former is a bit simple whereas the latter is rather complex. I'd like to be able to record activity on a screen, pause the recording while I gather my thoughts or segue to a different sequence, add captions (or maybe audio) and cut or append videos.

    Any suggestions?

  7. The description for 'add new tag' in (say) the field setup in the back-end UI reads 'You may use letters, digits or underscore.' It does not mention capital letters. However, if capital letters are included, they seem to get converted to lower case.

    On the other hand, via the API you can set $field->tags to have values which include capitals and these are then shown correctly in the UI and is maintained correctly in the API. But if you amend other data for the field (say) then  the tag is amended to the lower case version.

    Is this a bug? What should the proper behaviour be? It has certainly caught me out - assigning tags with capitals in the API which work fine for a while until some unrelated part of the field is changed and then the tag no longer matches the original.

  8. Thanks for all the tips, @Robin S. However one of the fields I need is a repeater matrix and that doesn't seem to support autojoin. Also, while I could get autojoin to work (for other fields) via the field settings, I couldn't replicate your example in my system - none of the requested ('autojoinable') fields were joined. I am on PW 3.0.206.

    findRaw() works as documented and gets all the fields and may be usable for my purposes, but that doesn't feel like the right answer.

    It seems like findJoin might be a bit buggy - I'm not sure @adrian's issue 

     has been resolved.

    But it may be something strange in my set up.

  9. Thanks @Robin S . Sort of ‘doh’ as I had heard of autojoin, but never used it or looked into it. That post explains everything, although I wonder if some fuller documentation in the API for $page might be helpful. I guess using autojoin is more efficient than getting the page then using getFields(). 

  10. Having used PW for a few years now, I feel that this is a really basic question about something that ought to be obvious to me! So I'm prepared to say 'doh!'

    If I get a page object via the $page API (or page() or $pages or whatever) it doesn't always seem to have the fields fully populated.

    For example, here is the 'Field List & Values' display (partial) for a page 'gallery-of-apples', it has 5 fields:

    1267103997_gallerypageexamplefields.thumb.jpg.be25407eb35ff4c740907ba0412f4d3b.jpg

    If I call $page and dump it, all I get is the title field, but by getting the fields via getFields() and setting them, I get the complete picture - see Tracy console image below:

    118297960_galleryconsole.thumb.jpg.76c8662bfd94fd28caab7be79d62b511.jpg

    What is going on here? Why don't I get all the fields and values with $page?

  11. This is  a s lightly obscure issue, but hopefully very easy to deal with. If a page only has one child, then the child doesn't necessarily have a sort attribute. In this case, BatchChildEditor will add a sort attribute and save the page. This seems to me to be unnecessary (and is causing a small problem with a module of mine). Is it possible and reasonable to not set the sort if there is only one child?

    The relevant code is at the end of the saveChildren() method:

    $cp->sort = $i;
    
    if($cp->isChanged()) {
        $cp->of(false);
        $cp->save();
    }

    I think it would need something like:

    if($this->wire('input')->post->individualChildTitles->count() > 1) {
        $cp->sort = $i;
    }
    
    if($cp->isChanged()) {
        $cp->of(false);
        $cp->save();
    }

    but maybe there is a better solution?

    • Like 1
  12. 1 hour ago, ryan said:

    Ask it questions like how to develop a ProcessWire module, how to build a search engine in ProcessWire, how to use pagination in ProcessWire, or anything you can think of. It seems to have an answer for everything.

    Probably because ProcessWire has such a nice well-documented API 😀. I played with it a bit and it knows how to build modules, but when I asked it to build an inputfield module, the code was what you might use for an inputfield on a page. I am trying to think of how to educate it with the correct context 🤔

    • Like 5
  13. On 12/22/2022 at 9:31 AM, MoritzLost said:

    Were you viewing the form while logged in as superuser?

    Yes. I realised after my last update that was probably the cause and that there is no delay in setting up. Maybe a small update to the module documentation? Anyway a really helpful module and very easy to install in Formbuilder. Thanks a lot!

    • Like 1
  14. I have a little home-made pagebuilder that is based on RockFrontend and Tailwind and have a few observations that might (or not) be of interest.

    1. Tailwind works really well with Latte. The result is (to my mind) concise and understandable code that is easily encapsulated.
    2. I tried some of the plugin components etc. but was generally disappointed. As is usually the case with these things, you get a load of baggage you didn't really want and then you try and customise it slightly and it is not easy. I ended up just building my own components with Latte, Tailwind and a bit of custom vanilla js (with a bit of help from various examples on the www). For example, I built a general-pupose carousel template that can be called via a Latte {include file} with options, e.g.:
      {include 
          $config->paths->templates . 'motif_layouts/latte_blocks/motif_carousel.latte', 
          imagePages: $page->motif_image_page, 
          modal: true,
      }

      and 

      {include 
          $config->paths->templates . 'motif_layouts/latte_blocks/motif_carousel.latte',
          imagePages: $page->motif_image_page,
          start: $page->motif_image_page->first,
          autoCycle: ['speed' => 5000]
      }
    3. My pagebuilder makes extensive use of css grid-area-templates which are not present in Tailwind. I thought this might be a bit of a problem, but was pleasantly surprised that the css file reduced from this:
      .image-text .has-image {
        /*grid-area: content;*/
        display: grid;
        grid-template-columns: auto;
        gap: 20px;
        grid-template-areas:
          "image"
          "text";
      }
      
      @media (min-width: 500px) {
      
        .image-text .has-image:not(.image-right) {
          /*grid-area: content;*/
          display: grid;
          grid-template-columns: 1fr 5fr;
          gap: 20px;
          grid-template-areas:
          "image text"
          "....  text";
        }
      
        .image-text .has-image.image-right {
          /*grid-area: content;*/
          display: grid;
          grid-template-columns: 5fr 1fr;
          gap: 20px;
          grid-template-areas:
          "text image"
          "text  ....";
        }
      
      }
      .image-text .no-image{
        /*grid-area: content;*/
        display: grid;
        grid-template-columns: auto;
        grid-template-areas:
          "text";
      }
      
      .image-text img{
        grid-area: image;
      }
      .image-text div#body{
        grid-area: text;
      }

      to this:

      .image-text {
        --stacked:      "image"
                        "text";
        --image-left:   "image text"
                        "....  text";
        --image-right:  "text image"
                        "text  ....";
        --text-only:         "text";
      }
      
      .image-text img{
        grid-area: image;
      }
      .image-text div[id*='body']{
        grid-area: text;
      }

      with only a marginal increase in the html (using arbitrary properties and values) and (I think) an increase in clarity:

      <div n:class= "$imgs > 0 ? '[grid-template-areas:var(--stacked)]' : '[grid-template-areas:var(--text-only)]', 
                    ($imgs > 0 && $page->motif_toggle == 0) ? 'sm:[grid-template-areas:var(--image-right)] sm:grid-cols-[5fr_1fr]',
                    ($imgs > 0 && $page->motif_toggle == 1) ? 'sm:[grid-template-areas:var(--image-left)] sm:grid-cols-[1fr_5fr]', 
                    grid, gap-5">

      Note that you need to include these type of class names in quote marks or Latte gets confused.

    4. The bit that worried me most was where I needed styling to be dependent on php variables. Some commentators have said that this presents a problem for Tailwind because it creates the stylesheet before the variables are known. However, the work-round is quite simple, with an in-line style tag to create css vars like this:
       

      <style>
        #gallery-item-{$imgPage->id} {
          --w: {$imgDisplayWidth|noescape}{$heightArray[1]}; 
          --w2: {$expandWidth|noescape}{$heightArray[1]};
          --wh: {$halfWidth|noescape}{$heightArray[1]};
          --wq: {$quarterWidth|noescape}{$heightArray[1]};
          --h: {$height|noescape};
          --h2: {$expandHeight|noescape};
        }
      </style>

      which then can be used directly:

      class="w-[var(--w)] hover:w-[var(--w2)]"

      It may be that there are better ways of doing some of this, but I was pleasantly surprised that each time I looked at a bit of slightly involved css and html and thought 'how the hell am I going to do that in Tailwind?', the answer was much shorter and clearer code. In particular, Latte works well with Tailwind throught the power of the n:class tag.
       

    • Like 5
×
×
  • Create New...