/** * Returns a list of the metatag fields on an entity. */ protected function getFields(ContentEntityInterface $entity) { $field_list = array(); if ($entity instanceof ContentEntityInterface) { // Get a list of the metatag field types. $field_types = $this->fieldTypes(); // Get a list of the fields on this entity. $fields = $entity->getFields(); // Iterate through all the fields looking for ones in our list. foreach ($fields as $key => $field) { // Get the field definition which holds the type. $field_info = $field->getFieldDefinition(); // Get the field type, ie: metatag. $field_type = $field_info->getType(); // Check the field type against our list of fields. if (isset($field_type) && in_array($field_type, $field_types)) { // Get the machine name of the field. $field_name = $field->getName(); $field_list[$field_name] = $field_info; } } } return $field_list; }
/** * Process an entity to either encrypt or decrypt its fields. * * Both processes are very similar, so we bundle the field processing part. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * @param string $op */ protected function process_entity(\Drupal\Core\Entity\ContentEntityInterface $entity, $op = 'encrypt') { // Make sure we can get fields. if (!is_callable([$entity, 'getFields'])) { return; } foreach ($entity->getFields() as $field) { $this->process_field($field, $op); } }