class TermReferenceField in Realistic Dummy Content 8
Hierarchy
- class \Drupal\realistic_dummy_content_api\attributes\Attribute
- class \Drupal\realistic_dummy_content_api\attributes\Field
- class \Drupal\realistic_dummy_content_api\attributes\TermReferenceField
- class \Drupal\realistic_dummy_content_api\attributes\Field
Expanded class hierarchy of TermReferenceField
1 file declares its use of TermReferenceField
- RealisticDummyContent.php in api/
src/ facade/ RealisticDummyContent.php - Define autoload class.
File
- api/
src/ attributes/ TaxonomyTermReferenceField.php, line 15 - Define autoload class.
Namespace
Drupal\realistic_dummy_content_api\attributesView source
class TermReferenceField extends Field {
/**
* {@inheritdoc}
*/
function ValueFromFile_($file) {
try {
$termname = $file
->Value();
if ($termname) {
return array(
\Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED => array(
array(
'tid' => $this
->GetTid($termname),
),
),
);
}
} catch (\Exception $e) {
return NULL;
}
}
/**
* Returns the term id for a term which is either existing or created on the fly.
*
* Let's say an entity (node) contains a term reference to the taxonomy vocabulary
* "location", and in the realistic dummy content file structure, "Australia" is
* used for the location. If "Australia" exists as a "location", then this function
* will return its tid. If not, the term will be created, and then the tid will be
* returned.
*
* @param $name
* The string for the taxonomy term.
*
* @return
* The associated pre-existing or just-created tid.
*
* @throws
* \Exception
*/
function GetTid($name) {
$vocabularies = taxonomy_get_vocabularies();
$field_info = field_info_field($this
->GetName());
$candidate_existing_terms = array();
foreach ($field_info['settings']['allowed_values'] as $vocabulary) {
$vocabulary_name = $vocabulary['vocabulary'];
foreach ($vocabularies as $vocabulary) {
if ($vocabulary->machine_name == $vocabulary_name) {
$tree = \Drupal::entityManager()
->getStorage('taxonomy_term')
->loadTree($vocabulary->vid);
$candidate_existing_terms = array_merge($candidate_existing_terms, $tree);
}
}
}
foreach ($candidate_existing_terms as $candidate_existing_term) {
if ($candidate_existing_term->name == $name) {
return $candidate_existing_term->tid;
}
}
if (!isset($vocabulary->vid)) {
throw new \Exception('Expecting the taxonomy term reference to reference at least one vocabulary');
}
$term_values['name'] = $name;
$term_values['vid'] = $vocabulary->vid;
$term = entity_save('term', $term_values);
if ($term->tid) {
return $term->tid;
}
else {
throw new \Exception('tid could not be determined');
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Attribute:: |
private | property | The entity is set on construction and is a subclass of EntityBase. It contains information about the entity to which this field instance is attached. | |
Attribute:: |
private | property | The name of this attribuet, for example title, picture, field_image... | |
Attribute:: |
function | Changes this attribute by looking for data in files. | ||
Attribute:: |
function | Given candidate files, change the value of this attribute based on one of them. | ||
Attribute:: |
function | Returns the appropriate environment, real or testing. | ||
Attribute:: |
function | Return a file object. | ||
Attribute:: |
function | Gets the bundle of the associated entity. | ||
Attribute:: |
function | Get all candidate files for a given field for this entity. | ||
Attribute:: |
function | Getter for $this->entity | ||
Attribute:: |
function | Get the entity type of the associated entity. | ||
Attribute:: |
function | Get acceptable file extensions which contain data for this attribute. | 2 | |
Attribute:: |
function | Return acceptable image file extensions. | ||
Attribute:: |
function | Getter for $this->name | ||
Attribute:: |
function | Return acceptable text file extensions. | ||
Attribute:: |
function | Gets the UID of the associated entity. | ||
Attribute:: |
function | Return an image file object if possible. | ||
Attribute:: |
function | Returns a pseudo-random number. | ||
Attribute:: |
function | Given a FileGroup object, get structured property if extentions ok. | ||
Attribute:: |
function | Given a list of files, return a value from one of them. | ||
Attribute:: |
function | Constructor. | ||
Field:: |
function |
Returns the type of this attribute. Overrides Attribute:: |
||
TermReferenceField:: |
function | Returns the term id for a term which is either existing or created on the fly. | ||
TermReferenceField:: |
function |
Given a FileGroup object, get a structured property Overrides Attribute:: |