1.2.3. M2_Content::put($contentType, $fields, [$options])

The putContent method on the $M2 object allows you to create new and update existing records for all content types in your system.

The $contentType parameter is equal to the one used in the getContent method - it is the id attribute of the contentType element in your XML declaration of the type.

The $fields parameter is what it's all about - this is an array filling in all fields from the XML declaration. Suppose we have a simple news type declared like this:

<?xml version="1.0" encoding="UTF-8"?>
<contentType id="news">
  <title lang="en" number="single">News</title>
  <title lang="en" number="plural">News</title>
  <title lang="da" number="single">Nyhed</title>
  <title lang="da" number="plural">Nyheder</title>

  <sorting direction="desc" fieldName="published" type="field" />

  <fields>
    <field id="heading" linkField="1" multiLingual="1" type="text">
      <title lang="da">Overskrift</title>
      <title lang="en">Heading</title>
      <rules>
        <required />
      </rules>
    </field>

    <field id="published" multiLingual="0" type="date">
      <title lang="da">Dato</title>
      <title lang="en">Date</title>
    </field>

    <field id="illustration" multiLingual="0" type="image">
      <title lang="da">Billede</title>
      <title lang="en">Picture</title>
    </field>

    <field id="body" multiLingual="1" type="htmlarea">
      <title lang="da">Brødtekst</title>
      <title lang="en">Body</title>
      <rules>
        <required />
      </rules>
    </field>
  </fields>
</contentType>

If you wish to create a record of this type via the putContent method, here's what is looks like:

<?php

$fields = array(
                'heading' => 'My Heading',
                'published' => 1217566800,
                'illustration' => array(
                                        'name' => 'image.jpg',
                                        'type' => 'image/jpeg',
                                        'tmp_name' => '/path/to/imagetmpname.jpg'
                                       ),
                'body' => "<p>Here's my body text.</p>'
               );

$options = array('action' => 'create');

M2_Content::put('news', $fields, $options);

?>

Creating a record in Moski2.net is simply a matter of providing the values for all fields an a hash. Fields of type image or file are slightly different: these must contain an array with the following keys:

Table 10. Keys for file based fields in putContent

KeyDescription
name

The name of the original file.

type

Mime type of the file.

tmp_name

Path where the file is located on the server.


The bright student will notice that these keys are similar to the ones available from the PHP $_FILES variable after doing a file upload - the intention is that you can pass on a file from $_FILES directly to putContent and not worry about mime types or how to get the file from your upload and into Moski2.net. Of course you are not restricted to this, but can supply the values manually if your file does not stem from an upload.

The method will return TRUE upon success, or throw an error if anything goes wrong. We try to honor any rules specified in your XML, but currently only required is checked, meaning that required fields must be filled in for putContent to succeed.

The $options parameter is an array holding any number of these keys.

Table 11. Keys for the options array

KeyDescriptionDefault
lang

Language of the content you are creating. This is only relevant for sites running multiple languages and can be omitted for sites that are not running in multilingual mode.

The current language of the site. If in doubt, this can be checked with $M2->getCurrentLanguage().
uid

ID of the user who will be marked as creator of this content.

0 or the ID of any user logged in when running the method.
action

'create' or 'update'.

'create'
nid

If action is set to 'update', provide the ID of the record here.

NULL

Note

A few things to keep in mind when "putting content" into Moski2.net:

  • Files can be no bigger than XXX MB

  • If dealing with a content type that has a parent type, you can provide the ID of the parent node in the '__parent__' key in $fields. If omitted, the record will start its life as an orphan.