'Initialize this form element for a particular record. Retrieves
the component data for this record from the database and
converts them into a JSON string to populate the input field with.
@param resource: the resource the record belongs to
@param record_id: the record ID
@return: the JSON for the input field.'

def extract(self, resource, record_id):

    self.resource = resource
    component_name = self.selector
    if (component_name in resource.components):
        component = resource.components[component_name]
        if component.link:
            component = component.link
        table = component.table
        tablename = component.tablename
        pkey = table._id.name
        fieldname = self.options['field']
        field = table[fieldname]
        if (pkey == fieldname):
            qfields = [field]
        else:
            qfields = [field, table[pkey]]
        items = []
        if record_id:
            query = ((resource.table._id == record_id) & component.get_join())
            if ('filterby' in self.options):
                f = self._filterby_query()
                if (f is not None):
                    query &= f
            rows = current.db(query).select(*qfields)
            iappend = items.append
            has_permission = current.auth.s3_has_permission
            for row in rows:
                row_id = row[pkey]
                item = {'_id': row_id}
                permitted = has_permission('read', tablename, row_id)
                if (not permitted):
                    continue
                permitted = has_permission('update', tablename, row_id)
                if (not permitted):
                    item['_readonly'] = True
                if (fieldname in row):
                    value = row[fieldname]
                    try:
                        text = s3_represent_value(field, value=value, strip_markup=True, xml_escape=True)
                    except:
                        text = s3_unicode(value)
                else:
                    value = None
                    text = ''
                value = field.formatter(value)
                item[fieldname] = {'value': value, 'text': text}
                iappend(item)
        data = {'component': component_name, 'field': fieldname, 'data': items}
    else:
        raise AttributeError('Undefined    component')
    return json.dumps(data, separators=SEPARATORS)
