Search code examples
javascriptmongodbapostrophe-cms

Apostrophe: Pulling in custom global settings into beforeConstruct for piece module (blog)


I am trying to pull in custom global settings I have created in the appropriate area that has an array field type.

I want to pull a global setting into the blog module to build out a field to add to the blog type. I tried using apostrophes model layer and the mongo db collections as described on the tutorial docs, but it would appear those need some req interaction or a self.method to fire correctly, which I don't think is available in the beforeConstruct function.

// This would ideally be a data.global.arrayField
var optionArraySet = ['Item 1', 'Item 2', 'Item 3'];
var formattedArraySet = [];
for (var type in optionArraySet) {
  formattedArraySet.push({
    label: optionArraySet[type],
    value: optionArraySet[type]
  })
}
options.addFields = [
  {
    label: 'Custom Array Field',
    name: 'customArrayField',
    type: 'select',
    choices: formattedArraySet
  }
].concat(options.addFields || [])

So optionArraySet would ideally by the global setting containing the data array I want to access.

For context, this global setting would apply to a couple different areas, and rather than have to update the code in the backend if we wanted to change the values, I could just add new values to the array set in the global settings and have the frontend pages update their display and settings fields update on the appropriate pieces.

If this helps, I am basically trying to control the tags that a content editor can pick on a particular piece type, so that they can't enter unwanted tags.

construct: function(self, options) {
 self.beforeSave = function(req, piece, options, callback) {
  var newTags = [];

  newTags.push(piece.anotherFieldValue);

  var customArrayField = piece.customArrayField;

  for (var option in customArrayField) {
    newTags.push(customArrayField[option]);
  }

  piece.tags = newTags;
  return callback();
 };
}

Thanks


Solution

  • Since global settings can be edited at any time, and beforeConstruct is only executed when the site starts up, it is not really useful to read the global settings there.

    There are ways you could patch the schema at runtime, but there is a much simpler solution:

    1. Create a new piece type module, "special-tags". Set name to special-tag.

    2. Use a joinByArray schema field named _specialTags to join withType: 'special-tag'.

    Now users can only pick from the list of special tags when editing a document that has this join in its schema, and admins can edit the special tags via the admin bar like any other piece type. The join loads with the document, so you can access, for instance, ._specialTags and find an array of docs with title properties containing the tag names.

    For more information about joinByArray please see:

    http://apostrophecms.org/docs/tutorials/getting-started/schema-guide.html#code-join-by-array-code

    And the schema guide in general.