0
votes

We all may know the following snippet that gets the first entry of the media field of a page to e.g. display a header image on a page.

10 = IMAGE
10.file {
  import = uploads/media/
  import.data = levelmedia: 0, slide
  import.listNum = 0
}

I am creating an extension (plugin, no ItemuserFunc etc) which needs to work with this media. I don't want to process dozens of typoscript directives in php to create this images. How can I use the example above to process the media field through typoscript, using core functionality instead of reinventing the wheel?

So now I'm in my extension code, having an array of page records. How to get further? The code below doesn't provide the functionality I need because I am not working on the page level (like I would when using the TS from above in a TMENU).

$content .= $this->cObj->stdWrap($page['media'], $this->conf['media_stdWrap.']);
1

1 Answers

1
votes

Having an array of page records ($pages) with the associative keys same as the field names in the database, mainly the media field, you would simply walk through the array and create the images using the Typoscript configuration.

Example 1 (a programmer sets the rendering)

foreach($pages as $value) {
  $mediaField = t3lib_div::trimExplode(',', $value['media']);
  if(!$mediaField[0]) continue;

  $imageConf = array(
    'file' => 'uploads/media/' . $mediaField[0],
  );
  $content .= $this->cObj->cObjGetSingle('IMAGE', $imageConf);
}

Example 2 (an editor sets the rendering)

foreach($pages as $value) {
  $this->cObj->data = $value;
  $content .= $this->cObj->stdWrap($value['media'], $this->conf['media_stdWrap.']);
}

It sets the page data so that they are available as field in Typoscript. The configuration would then look something like this:

{
  media_stdWrap {
    cObject = IMAGE
    cObject {
      file {
        import = uploads/media/
        import.field = media
        import.listNum = 0
      }
    }
  }
}

Example 3 (combination of the previous 2 examples)

foreach($pages as $value) {
  $mediaField = t3lib_div::trimExplode(',', $value['media']);
  $mediaOutput = '';

  // Creating the output with a default rendering
  if($mediaField[0]) {
    $imageConf = array(
      'file' => 'uploads/media/' . $mediaField[0],
    );
    $mediaOutput = $this->cObj->cObjGetSingle('IMAGE', $imageConf);
  }

  // Allowing custom Typoscript to completely modify the media part
  if(array_key_exists('media_stdWrap.', $this->conf)) {
    $this->cObj->data = $value;
    $mediaOutput = $this->cObj->stdWrap($mediaOutput, $this->conf['media_stdWrap.']);
  }

  $content .= $mediaOutput;
}

NOTE: These examples are completely untested. I've came up with them out of my head.