Posted by aidanlis on January 8, 2010 at 9:10am
Hi everyone,
I have code popping up like this in a few of my modules ... basically I go through and check all of the hardcoded profile and CCK fields I use actually exist.
It seems like there could be a better way to do it, any ideas?
<?php
function example_requirements($phase) {
$requirements = array();
if ($phase === 'runtime') {
// Check for the profile category
$categories = profile_categories();
$category_exists = FALSE;
foreach ($categories as $category) {
if ($category['name'] === EXAMPLE_PROFILE_AFFILIATE_CATEGORY) {
$category_exists = TRUE;
break;
}
}
// Check for the profile fields
$result = _profile_get_fields( EXAMPLE_PROFILE_AFFILIATE_CATEGORY);
$firstname_exists = FALSE;
$lastname_exists = FALSE;
while ($row = db_fetch_array($result)) {
if ($row['name'] === EXAMPLE_PROFILE_COMMISSION_RATES) {
$field_exists = TRUE;
}
}
// Throw the requirement
if (!$field_exists) {
$requirements['example_profile'] = array(
'title' => t('Example module'),
'description' => t('This module relies on the profile module being correctly configured, please see this modules internal documentation.'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Profile module incorrectly configured'),
);
}
// Check for the content type
$types = content_types();
if (!isset($types[EXAMPLE_TYPE])) {
$requirements['example_ct'] = array(
'title' => t('Example module'),
'description' => t('This module relies on the %type content type existing, please create this content type.', array('%type' => EXAMPLE_TYPE)),
'severity' => REQUIREMENT_ERROR,
'value' => t('Required content type is missing'),
);
}
// Check for cck fields which are implicitly used in the module code
if (isset($types[EXAMPLE_TYPE])) {
$checks = array(
'field_commissioning_fee',
'field_budget',
'field_bid',
'field_fee_received_date',
'field_commission_status',
'field_treatment_deadline',
'field_production_budget',
);
$fields = content_fields();
$missing_field = FALSE;
foreach ($checks as $check) {
if (!(isset($fields[$check]) && $fields[$check]['type_name'] === EXAMPLE_TYPE)) {
$missing_field = TRUE;
break;
}
}
if ($missing_field) {
$requirements['example_field'] = array(
'title' => t('Example module'),
'description' => t('This module relies on the %banner content type having a %field field, please create this field.', array('%banner' => EXAMPLE_TYPE, '%field' => $check)),
'severity' => REQUIREMENT_ERROR,
'value' => t('Required field is missing'),
);
}
}
}
return $requirements;
}
?>