Score:1

Create several fields from one array field

ro flag

I have a Drupal View based on one entity that is pulled from another database (it is a CiviCRM entity pulled into Drupal with the help of CiviCRM Entity Drupal module).

One of the fields I use is an array field (let's say it's called arrayfield), which outputs an array like item1, item2, item3 (items are strings) for each instance of the entity. The number of items (that is, the array length of arrayfield) is limited to a certain number for each instance of the entity.

Now I want to have each of these items available as a separate field in the Drupal View. Field name should be item1 etc. and field value should be 1 if item1 is existent in this arrayfield's array, and 0 if item1 is not existent there.

How do I achieve this? I though of several solutions:

  1. somehow including a function inside Drupal Views that will "explode" my array field into several other fields. I know the functionality is there, because right now I can filter arrayfield for specific values with a normal Drupal Views filter - that is, Drupal Views is searching through the arrayfield array. I can just not access this functionality for my purpose.
  2. using PHP Views -- I read it should not be used due to security reasons: https://ohthehugemanatee.org/blog/2013/12/26/44497-people-are-wrong-how-to-never-use-views-php/ as well as performance: https://www.drupal.org/project/views_php
  3. Creating a custom little Drupal module for splitting arrayfield. The problem is that it seems to create a new table with the split values. But I need to be able to access these values in a Drupal View based on an existing entity. That is, I cannot work with random tables because they will not be available in my Drupal view's Relationships.
  4. Cloning the existing entity (in CiviCRM database), splitting the values of arrayfield, creating a new entity attribute for each of these values (0 or 1 again), creating a relation between original entity and new entity, making the new entity available in Drupal via CiviCRM Entity module, and including the new entity in Drupal views via Relationships. The problem is, we would prefer not to change the original database too much, if possible.
  5. Unfortunately, it seems to be impossible to clone and change the entity on the Drupal side only, as CiviCRM Entity module does not copy the entity into Drupal, but only pulls the information from the original database.

Does anyone have a hint? The need to split an array doesn't seem so complex to me honestly, so there should be a simpler solution to this inside Drupal Views than creating additional entities. It is only a question of display, nearly, as no additional information is required.

Usecase/Goal: I have a system with one Drupal and one CiviCRM database, that I want to complement with some statistics. I created a Drupal View for each statistical question, which pulls its information from the CiviCRM database with the help of CiviCRM Entity Drupal module. But the data (which is multiple choice options) comes in as an array of options. I cannot properly count the options with Drupal Views if they are not available as single values.

Jaypan avatar
de flag
There is likely a more drupally solution to your issue, but you only told us the technical details of how you are trying to achieve some goal, but didn't explain the goal. If you explain the real-world use-case, someone may be able to provide the Drupal way to achieve it.
lk flag
I would raise this question in the civicrm_entity channel at https://chat.civicrm.org - that's where the folks who are working most actively with this module tend to hang out.
emma avatar
ro flag
My boss asked the question in the CiviCRM Chat, and got the advice to change the way the multiple choice data is stored in CiviCRM. I figured out before that the way CiviCRM stores multiple choice values is weird/non-recommended (see https://dba.stackexchange.com/questions/291361/transform-a-varchar-field-with-delimiters-into-multiple-rows?noredirect=1#comment570065_291361 ). But changing the CiviCRM database seems to be a very tough solution for splitting an array field in Drupal Views.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.