Which one is better for patterns? I'm comfortable with XML but is there any pros and cons between the two options as far as functionality?

Also, I could use some advanced tutorials on how to automate setup of various items in a site.
1. A pattern to setup the sitewide contact form. Creating a default category by using patterns to specify the required fields.
2. A pattern to create feed importers for the Feeds module.

Anyone have success doing this and care to share instruction? Particularly in XML?
Thanks.

Comments

ChrisBryant’s picture

It's really more a matter of your personal preference and what you would prefer to work with. To the module it doesn't make much difference. Yaml patterns are automatically converted to XML using the Spyc library so XML is still the native format the module uses.

The main reason for using the Yaml format is that they are easier to read/maintain. If that doesn't matter to you then you'll be fine with using XML.

To create Patterns for modules that don't directly support Patterns (or don't have pattern component files such as Feeds) you can use the form tag. Here is some documentation on it:

http://drupal.org/node/516332

ChrisBryant’s picture

Slight correction, Yaml patterns are converted directly to PHP arrays rather than Yaml to XML to PHP. I should have said that XML is the "default" format rather than "native" as both the Yaml and XML are converted to PHP arrays.

vaish’s picture

XML can't handle numeric values as XML tags which then requires a workaround (see #613270: Syntax for XML patterns which enables defining numeric array keys in XML)

YAML is cleaner and more straightforward to write but more difficult to validate. Wrong indentation may cause YAML file to produce unexpected PHP array while still being perfectly valid YAML file. Also, spyc library that we use for parsing YAML doesn't work well with comments and empty lines in between the yaml code.

I would say if you are already comfortable with XML, just stick to it.

vaish’s picture

Component: Miscellaneous » Documentation
Status: Active » Needs work

Probably good idea to add this to documentation.

apotek’s picture

Interesting. Why does line 215 of patterns.module say:
drupal_set_message(t('Import feature currently supports only XML file format.'), 'warning');

Also, line 524:
drupal_set_message(t('Import feature currently supports only XML file format.'), 'warning');

The validation function of patterns_import_validate only accounts for XML.

so my assumption has been that if I want to import YAML, I have to run it through spyc, to get it into an array, then convert to XML from the array, so that I can use the native import functions. That's all I see available.

I'm missing something big here. It's a roadblock for my work on drush support. Don't quite understand how i can work with yaml if I can't import them. How does patterns module know about and use what it can't import?

ChrisBryant’s picture

As far as I understand those messages are only there for the Import functionality that is available from the Patterns UI. You shouldn't need to "import" patterns from the UI and rather you can just put them on your site file system in locations that they will be picked up from. For instance, you can write a foo.xml or foo.yaml file and put it in the /sites/{default|all|example-site}/patterns directories or /sites/{default|all|example-site}/modules/patterns/patterns and the module will automatically pick up the files and make them available for you. That should be the preferred method of adding patterns to your site.

When using the admin ui to import patterns the files are saved into the /sites/example-site/files/patterns directory instead.

Ideally the module would support also importing yaml files in the admin UI so I just created a feature request for it:

#805256: Allow Yaml pattern files to be imported using the admin UI

vaish’s picture

@klktrk Import functionality was implemented much before we added support for YAML and was never updated to support YAML. Warning has been added to ensure users will be aware of such limitation.

As ChrisBryant already explained import is just an additional feature and patterns could work equally well even without it. We prefer to add our patterns via file system and keep them under version control. Import feature saves the patterns into Drupal's files directory (because it needs write access) which is not very compatible with keeping files under version control. It's targeted more towards users that are not comfortable with managing their files directly and don't use version control.

Please take a look at patterns_get_patterns() function. That's the function that will scan predefined set of directories and search for available patterns. For each pattern file that has been found it will call patterns_load_xml() or patterns_load_yaml() depending on the file extension. Those two functions are responsible for converting the patterns files into PHP arrays. Additionally, you can keep your pattern in a php file. That will be loaded by calling patterns_load_php(). No conversion occurs here, your php file should already contain an array that represents valid pattern.

Let me know if you have any questions.