Tagged: 

Viewing 13 reply threads
  • Author
    Posts
    • #2854
      cosmocanuck
      Member

      Hi guys. I’ve created a back-end “Recipe” CPT, and for the posts, I want to be able to potentially have additional sections for different parts of the recipe, i.e. when a salad recipe lists the ingredients for the dressing separately (and also has two areas in the instructions, one for the dressing and one for the salad).

      I figured I’d do nested groups: i.e. for the ingredients I’d have an add_more group called “Component”, inside of which would be another add_more group for adding ingredients; each ingredient is a pair of fields within that, for “amount” and “item”. Similar construction for the instructions part of the recipe.

      I swear I almost had it, but I found that the fields were, weirdly, arranged very strangely, in concentric circles, on the back end. Now, I can’t even re-create that.

      Here’s my code as it stands now:

      piklist('field', array(
          'type' => 'group'
          ,'field' => 'component'
          ,'label' => __('Components')
          ,'description' => __('Add additional Components if a recipe has separate listings for different parts, i.e. the salad and the dressing.')
          ,'add_more' => true    
          ,'fields' => array(
      					'type' => 'group'
      					,'field' => 'ingredient'
      					,'label' => __('Ingredients')
      					,'description' => __('For consistency, please only capitalize sentences and Superior product names; otherwise keep all text lower case.')
      					,'add_more' => true
      					,'fields' => array(
      						array(
      							'type' => 'text'
      							,'field' => 'ingredient_qty'
      							,'label' => __('Quantity')
      							)
      
      						,array(
      							'type' => 'textarea'
      							,'field' => 'ingredient_descrip'
      							,'label' => __('Description')
      							)
      						)
      					)
      ));

      I’ve studied this but can’t figure out where I’m going wrong. Or am I trying to do the impossible?

      Thanks!
      Adam

    • #2855
      Marcus
      Member

      Hey cosmocanuck.
      Other than surrounding your first fields array within another array (as FIELDS arrays, need two)
      I can’t see a thing wrong with your code, however I took it and tested it and sure enough, only the inside group is add-moreing. 🙂

      Don’t quite know whats wrong.

      Marcus

    • #2862
      Steve
      Keymaster

      @cosmocanuck– You were pretty close. Group fields are really, really powerful, but sometimes the layout gets tricky. A few tips:

      -It’s best to use the column parameter for layout. Piklist has a built-in 12 column grid system.
      -Actual fields in WordPress do not have descriptions, just the main field name. I replaced one of your field descriptions with a tooltip.
      HTML fields are awesome when you don’t want to enter content, but only show it… including labels.
      -Try to use the code in Piklist Demos as a starting point. I used Advanced > Content Section (Grouped).

      Take a look at the attached screenshot to make sure I got it right. Here’s the code:

        piklist('field', array(
          'type' => 'group'
          ,'field' => 'component'
          ,'label' => __('Components')
          ,'description' => __('Add additional Components if a recipe has separate listings for different parts, i.e. the salad and the dressing.')
          ,'add_more' => true  
          ,'fields' => array(
            array(
              'type' => 'html'
              ,'label' => __('Ingredients')
              ,'help' => __('For consistency, please only capitalize sentences and Superior product names; otherwise keep all text lower case.')
              ,'columns' => 12
              ,'attributes' => array(
                'class' => 'large-text'
              )
            )
            ,array(
              'type' => 'group'
              ,'field' => 'ingredient'
              ,'add_more' => true
              ,'fields' => array(
                array(
                  'type' => 'number'
                  ,'field' => 'ingredient_qty'
                  ,'label' => __('Qty')
                  ,'columns' => 1
                )
                ,array(
                  'type' => 'textarea'
                  ,'field' => 'ingredient_descrip'
                  ,'label' => __('Description')
                  ,'columns' => 11
                )
              )
            )
          )
        ));
      
      Attachments:
      You must be logged in to view attached files.
    • #2865
      cosmocanuck
      Member

      Thanks Steve, that’s awesome. I even managed to further customize it with a title field to name each component.

      It was tricky though… bit of a pretzel logic thing… and I did look for a rundown on the use of Groups and Columns in the tutorials section, but couldn’t seem to find anything that really explains it – it’s listed here:

      https://piklist.com/user-guide/docs/field-parameters/

      But it doesn’t really explain when they “kick in” and how to use them… might be something for a future embellishment to the User Guide…

      But bottom line, thanks a million for helping with this, and I will certainly study, and hopefully learn from, your example!

      adam

    • #2868
      Steve
      Keymaster

      @cosmocanuck– would you mind posting your code? I would like to put it in the Demos.

    • #2870
      cosmocanuck
      Member

      Sure, Steve! Here you go:

      piklist(‘field’, array(
      ‘type’ => ‘group’
      ,’field’ => ‘component’
      ,’label’ => __(‘Components’)
      ,’description’ => __(‘Add additional Components if a recipe has separate listings for different parts, i.e. the salad and the dressing.’)
      ,’add_more’ => true
      ,’fields’ => array(
      array(
      ‘type’ => ‘html’
      ,’label’ => __(‘Ingredients’)
      ,’help’ => __(‘For consistency, please only capitalize sentences and Superior product names; otherwise keep all text lower case.’)
      ,’columns’ => 12
      ,’attributes’ => array(
      ‘class’ => ‘large-text’
      )
      )

      ,array(
      ‘type’ => ‘text’
      ,’field’ => ‘component_title’
      ,’label’ => __(‘Component Title (optional for 1st)’)
      ,’columns’ => 12
      )

      ,array(
      ‘type’ => ‘group’
      ,’field’ => ‘ingredient’
      ,’add_more’ => true
      ,’fields’ => array(
      array(
      ‘type’ => ‘number’
      ,’field’ => ‘ingredient_qty’
      ,’label’ => __(‘Qty’)
      ,’columns’ => 2
      )
      ,array(
      ‘type’ => ‘textarea’
      ,’field’ => ‘ingredient_descrip’
      ,’label’ => __(‘Description’)
      ,’columns’ => 10
      )
      )
      )
      )
      ));

    • #2872
      cosmocanuck
      Member

      Never mind! It was the code, I hadn’t noticed the qty was set to a “Number” field, so it was stripping out the content when I added text to that field. It actually needed to be a text field since the quantity often includes words, i.e. “1 tsp”.

      Sorry about the panic!

    • #2873
      cosmocanuck
      Member

      OK, this is not a panic but a real problem, hopefully just a code error but for the life of me I can’t find it!

      I duplicated the same overall structure that works for the ingredients, into the instructions area. So, I can have multiple “groups” of instructions if necessary.

      However, when I add multiple steps – when I save the post, each step gets wrapped in its own “group” even though I did not add any additional groups!

      Here’s my code for that section, as it currently stands. Login info if you need it, is in the above post in this thread.

      Thanks!

      piklist('field', array(
         'type' => 'group'
         ,'field' => 'steps_section'
         ,'label' => __('Steps')
         ,'description' => __('Do not add numbers to the steps; they\'ll be added automatically.')
         ,'add_more' => true  
         ,'fields' => array(
      					array(
      				   'type' => 'html'
      				   ,'label' => __('Steps section')
      				   ,'columns' => 12
      				   ,'value' => ''
      				   ,'attributes' => array(
      					 'class' => 'recipe-subhead'
      										)
      					 )
      	
      	
      	
      	
      				 	,array(
      						'type' => 'text'
      						,'field' => 'steps_component_title'
      						,'label' => __('Section Title (can be omitted if there’s only one section)')
      						,'columns' => 12
      					)
      
      					 ,array(
      						'type' => 'textarea'
      						,'field' => 'recipe_step'
      						,'label' => __('Recipe Step')
      						,'columns' => 12
      						,'add_more' => true
      				 	 )
      				 	 
      				 	 
      				 	 
      				 	 	 
      	
         				)
       ));
    • #2874
      Steve
      Keymaster

      If it works in one place then it’s obviously an error.

      Try duplicating the code again. Also verify that you changed all the field names. Duplicate field names on the same page can make everything blow up.

    • #2875
      cosmocanuck
      Member

      Thanks Steve. I finally realized what I did – I didn’t keep the ingredient steps within a Group field as I’d done for the Qty/Ingredient pairs. I’d thought that being a single repeating field, it would stay in its containing Group, but clearly it needed to still be in a group even if is only one repeating item, not a set.

      And, I’d say this support thread is done! I may have another for you about displaying the nested data… but I’ll start that up separately if needed. 8^)

      Thanks!

    • #2876
      cosmocanuck
      Member

      Oh, BTW, please could you delete my post with the Dashboard access info? I meant to set that as private… don’t want it on the forums of course.

      Last but not least, as requested, here’s my final working code for the full “recipe entry” form:

      <?php
      /*
      Title: Recipe Details
      Post Type: recipe
      Priority: high
      */
      
      piklist('field', array(
          'type' => 'editor'
          ,'field' => 'recipe_description'
          ,'template' => 'field'
          ,'label' => __('Description')
          ,'description' => __('Brief description/summary of the recipe')
          ,'options' => array(
      		'drag_drop_upload' => 'false'
      		,'tinymce' => true
      		,'quicktags' => true
        		)
      ));
      
      piklist('field', array(
         'type' => 'group'
         ,'field' => 'ingredient_section'
         ,'label' => __('Ingredients')
         ,'description' => __('Add additional Section if a recipe has separate listings for different parts, i.e. the salad and the dressing.')
         ,'add_more' => true
         ,'fields' => array(
      					array(
      				   'type' => 'html'
      				   ,'label' => __('Ingredient section')
      				   ,'columns' => 12
      				   ,'value' => '<p><em><strong>Note:</strong> for consistency, please only capitalize sentences and Superior product names; otherwise keep all text lower case.</em></p>'
      				   ,'attributes' => array(
      					 'class' => 'recipe-subhead'
      										)
      					 )
      				 
      					 ,array(
      						'type' => 'text'
      						,'field' => 'ingredients_component_title'
      						,'label' => __('Section Title (can be omitted for first section, or if there&rsquo;s only one)')
      						,'columns' => 12
      						)
      				 
      					 ,array(
      					   'type' => 'group'
      					   ,'field' => 'ingredient'
      					   ,'add_more' => true
      					   ,'fields' => array(
      							 array(
      							   'type' => 'text'
      							   ,'field' => 'ingredient_qty'
      							   ,'label' => __('Qty')
      							   ,'columns' => 2
      							 )
      							 ,array(
      							   'type' => 'textarea'
      							   ,'field' => 'ingredient_descrip'
      							   ,'label' => __('Description')
      							   ,'columns' => 10
      							 )
      				   		)
      				 )
         )
       ));
       
       piklist('field', array(
         'type' => 'group'
         ,'field' => 'steps_section'
         ,'label' => __('Steps')
         ,'description' => __('Do not add numbers to the steps; they\'ll be added automatically.')
         ,'add_more' => true  
         ,'fields' => array(
         
         
         
      					array(
      				   'type' => 'html'
      				   ,'label' => __('Group of steps')
      				   ,'columns' => 12
      				   ,'value' => ''
      				   ,'attributes' => array(
      					 'class' => 'recipe-subhead'
      										)
      					 )
      	
      		
      				 	,array(
      						'type' => 'text'
      						,'field' => 'steps_component_title'
      						,'label' => __('Section Title (can be omitted if there&rsquo;s only one section)')
      						,'columns' => 12
      					)
      
      					 ,array(
      					   'type' => 'group'
      					   ,'field' => 'ingredient'
      					   ,'add_more' => true
      					   ,'fields' => array(
      							 array(
      								'type' => 'textarea'
      								,'field' => 'recipe_step'
      								,'label' => __('Recipe Step')
      								,'columns' => 12
      								,'add_more' => true
      							 )
      				   		)
      				)
      	)
      
       ));
    • #2888
      Steve
      Keymaster

      This will appear in the next major version of Piklist Demos. Thanks!

    • #2909
      cosmocanuck
      Member

      Thank YOU! It’s an honour!

    • #2916
      Steve
      Keymaster

      Same here. 😉

Viewing 13 reply threads
  • The topic ‘Nested groups problem’ is closed to new replies.