2

1. Setup

We've added two layout dropdown fields to the page_list block's edit screen by overriding:

  • db.xml
  • page_list_form.php
  • Adding the fields to the view

2. In db.xml we've added:

<field name="gridSize" type="C" size="255">
</field>
<field name="gridPaddingStyle" type="C" size="255">
</field>

3. In page_list_form.php

We've added the slect fields to the block edit screen like this:

  <div class="ccm-block-field-group">
    <h2><? echo t('Grid layout')?></h2>

    <p><? echo t('Grid size')?></p>
      <?php
          $gridSize = array(
          '1up' => 'grid-list-item-single grid-unit-1',
          '2up' => 'grid-unit-2',
          '3up' => 'grid-unit-3',
          '4up' => 'grid-unit-4',
          '5up' => 'grid-unit-5',
          '6up' => 'grid-unit-6'
          );

          if (is_array($gridSize)) :
      ?>
      <select name="gridSize" id="selectGridSize">
          <? foreach ($gridSize as $gridItem => $value) : ?>
          <option value="<?= $value ?>" <?php if ($gridSize == $value) { ?> selected <?php } ?>>
          <?= $gridItem ?>
          </option>
          <? endforeach; ?>
      </select>
      <? endif; ?>

      <p><? echo t('Grid padding style')?></p>
      <?php
          $gridPaddingStyle = array(
          'Padding' => '',
          'No padding' => 'grid-no-padding',
          'Hairline' => 'grid-hairline'
          );

          if (is_array($gridPaddingStyle)) :
      ?>
      <select name="gridPaddingStyle" id="selectPaddingSize">
          <? foreach ($gridPaddingStyle as $gridPaddingStyleItem => $value) : ?>
          <option value="<?= $value ?>" <?php if ($gridPaddingStyle == $value) { ?> selected <?php } ?>>
          <?= $gridPaddingStyleItem ?>
          </option>
          <? endforeach; ?>
      </select>
      <? endif; ?>
  </div>

4. In the view.php we've added:

$gridSize = $controller->gridSize;
$gridPadding = $controller->gridPaddingStyle;

Which obviously pulls the data out of the database for use when we output the markup.


Everything works great except when we come to re-edit the block - essentially the values previously set for our custom fields don't get read and the dropdowns revert back to the first items in the select lists.


5. Question

How do we get the page_list edit screen to read the values previously set in the database?


Any pointers in the right direction would be much appreciated (sorry, can't work out how to get syntax highlighting working - wish the markdown was the same ad Github issues).

Cheers

Ben

CMSCSS
  • 2,076
  • 9
  • 28
  • 49

1 Answers1

2

Your problem here is that you're overwriting the variables in your edit view $gridSize = array(...) just before you're trying to access them, you can probably fix it by changing those array variable names to something semantic like "grid_size_options".

If that doesn't work, you can use $this->set to pass variables to the view from the controller edit method. Your edit method would look something like this:

public function edit()
{
    $this->set('grid_size', $this->gridSize);
    $this->set('grid_padding_style', $this->gridPaddingStyle);
}

and then your edit view can just magically access $grid_size and $grid_padding_style.

 <select name="gridPaddingStyle" id="selectPaddingSize">
     <?php 
     foreach ($gridPaddingStyle as $gridPaddingStyleItem => $value) {
         ?>
         <option value="<?= $value ?>" <?= $grid_padding_style == $value ? 'selected' : '' ?> >
             <?= $gridPaddingStyleItem ?>
         </option>
         <?php
    }
    ?>
</select>
Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
  • Thanks heaps Korvin, you're absolutely right. Unfortunately, we're confused about how to work with alternate variable names (like "grid_size_options") and still pull the correct field out of the database - php is not our specialty! Will try overriding the controller as suggested. – CMSCSS Apr 22 '15 at 02:52
  • The options are not coming from the database, you're setting it in your example `$gridSize = array(`, just change that variable name and you will not be overwriting it anymore. You should reread my full comment because it answers your questions. – Korvin Szanto Apr 22 '15 at 02:56
  • Thanks Korvin - I think you would be surprised at how many times I read your answer! It all works thank you but I still don't understand how it grabs the items for the select list array initially - but then uses the database if something is already set? is that what this does? ``` – CMSCSS Apr 22 '15 at 03:06
  • The options aren't coming from the database, you're only saving the _one option that people choose_. Look at lines 6-13 in that link I provided, you are giving it a list of options. – Korvin Szanto Apr 22 '15 at 03:10
  • But at some point a selection is saved to the database - right? And this the line that compares what is in the database and selects it from the list? ``` – CMSCSS Apr 22 '15 at 03:52
  • Yes. If you select and save the first option, when `$value` is "1up", that option will be automatically selected. – Korvin Szanto Apr 22 '15 at 15:43